express.js - 基本应用路由器不工作
express.js - basic application router not working
我只是在创建一个基本的应用程序,但它似乎不适合我。谁能帮我找出错误?
这是我的代码:
import express from "express";
const app = express();
const router = express.Router();
app.use((req, res, next) => {
console.log("first middleware");
next();
});
router.get("/a", (req, res, next) => {
res.send("Hello this is route a");
});
router.post("/c", (req, res, next) => {
res.send("Hello this is route c");
});
app.listen({ port: 8000 }, () => {
console.log("Express Node server has loaded");
});
节点版本:v14.17.5
速成版:^4.17.1
提前致谢。
您需要应用程序才能使用路由器。
尝试添加 app.use('/route', router);
使用app.use
示例
app.use('/c', c);
使用路由器
import express from "express";
const app = express();
const router = express.Router();
router.get("/a", (req, res, next) => {
res.send("Hello this is route a");
});
router.post("/c", (req, res, next) => {
res.send("Hello this is route c");
});
app.use(router, (req, res, next) => {
console.log("first middleware");
next();
});
app.listen({ port: 8000 }, () => {
console.log("Express Node server has loaded");
});
我只是在创建一个基本的应用程序,但它似乎不适合我。谁能帮我找出错误?
这是我的代码:
import express from "express";
const app = express();
const router = express.Router();
app.use((req, res, next) => {
console.log("first middleware");
next();
});
router.get("/a", (req, res, next) => {
res.send("Hello this is route a");
});
router.post("/c", (req, res, next) => {
res.send("Hello this is route c");
});
app.listen({ port: 8000 }, () => {
console.log("Express Node server has loaded");
});
节点版本:v14.17.5
速成版:^4.17.1
提前致谢。
您需要应用程序才能使用路由器。
尝试添加 app.use('/route', router);
使用app.use
示例
app.use('/c', c);
使用路由器
import express from "express";
const app = express();
const router = express.Router();
router.get("/a", (req, res, next) => {
res.send("Hello this is route a");
});
router.post("/c", (req, res, next) => {
res.send("Hello this is route c");
});
app.use(router, (req, res, next) => {
console.log("first middleware");
next();
});
app.listen({ port: 8000 }, () => {
console.log("Express Node server has loaded");
});