我正在try 用NodeJS和Express创建一条简单的Hello World路由.当我运行node index.js
时,我在控制台中得到listening on port 3000
,但当我转到http://localhost:3000/helloworld
时,什么都不起作用.它一直在try 连接.
我可能在路由的某个地方遗漏了什么.
index.js
个
const express = require("express");
const app = express();
const helloworld_route = require("./routes/helloworld");
const dotenv = require("dotenv");
dotenv.config({ path: "../.env.sample" });
const port = process.env.PORT;
app.use(express.json);
app.listen(port, () => {
console.log(`Listening on ${process.env.PORT}`);
});
app.use("/", helloworld_route);
controllers/hello.js
个
const helloTest = (req, res) => {
res.send("Hello World!");
console.log("hello");
};
module.exports = {
helloTest,
};
Routes.helloworld.js
const express = require("express");
const router = express.Router();
const HelloWorld = require("../controllers/hello");
router.get("/helloworld", HelloWorld.helloTest);
module.exports = router;