且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

nodejs Route.get()需要回调函数,但得到了一个[object String]

更新时间:2022-06-11 17:22:50

问题似乎出在您如何安装路由器.查看路由器中间件API ,看来您应该这样做像这样

The problems seems to be how you are mounting the router. Looking the the router middleware API it seems you should be doing it like this.

test.js

const express = require('express');
const router = new express.Router();

router.get('/test', (req, res, next) => {
  res.send("I'm a test");
  next();
});

module.exports = router;

server.js

server.js

const express = require('express');
const app = express();
const test = require('./test'); 

app.use('/', test);

app.listen(3000);