nodejs Route.get()需要回调函数,但得到了一个[object String]
问题描述:
我开始使用带有express的nodejs进行编码. 所以我在文件test.js中做到了这一点,该文件位于我的文件夹路由中:
I'm starting coding using nodejs with express. So i did this in my file test.js which is into my folder routes :
const express = require('express');
const router = new express.Router();
router.get('/test', (req, res) => {
res.send(`I'm a test`);
});
module.exports = router;
然后,在我的server.js中:
Then, in my server.js :
const test = require('./server/routes/test');
app.use('/test', test);
在我的浏览器中,它告诉我无法获取/测试
In my browser, it tells me Cannot get/test
我不明白为什么.需要帮助. 谢谢
I don't understand why. Need for help. Thanks
答
问题似乎出在您如何安装路由器.查看路由器中间件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);