nodejs Route.get()需要回调函数,但得到了一个[object String] [英] nodejs Route.get() requires callback functions but got a [object String]

查看:280
本文介绍了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);

这篇关于nodejs Route.get()需要回调函数,但得到了一个[object String]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆