首页 > 解决方案 > Express API enpoint localhost:3000/xxx 不起作用,但 localhost:3000/ 是

问题描述

我想用express编写一个 API ,但我一开始就被卡住了。当我访问它时localhost:3000,它接缝可以工作,但是一旦我写localhost:3000/hello了一个 404 错误,就会发生(Cannot GET /hello)。

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/", (req, res) => {
    res.json({ message: "Welcome to application." });
});

app.get("/hello", (req, res) => {
    res.json({ message: "Welcome to hello." });
});

app.listen(3000, () => {
    console.log("Server is running on port 3000.");
});

标签: node.jsexpressrest

解决方案


推荐阅读