Route.all()需要callback函数,但得到)=>,然后显示404错误

我最近开始看节点Js,我的问题是关于快速路由。

我有一个dishRouter.js:

var express = require('express'); var dishRouter = express.Router(); var bodyParser = require('body-parser'); dishRouter.use(bodyParser.json()); dishRouter //.route('/dishes') .all('/dishes', function(req, res, next){ res.writeHead(200, {'Content-Type': 'application/json'}); next(); }) .get('/dishes', function(req, res, next){ res.end('will send all dishes to you'); }) .get('/dishes/:dishId', function(req, res, next){ res.end('will send the dish '+ req.params.dishId + ' to you'); }) .post('/dishes', function(req, res, next){ res.end('will add the dish '+ req.body.name + ' with details ' + req.body.description); }) .put('/dishes/:dishId', function(req, res, next){ res.write('Updating the dish '+ req.params.dishId+ ' '); res.end(' Updating the dish '+ req.body.name + ' with details '+ req.body.description); }) .delete('/dishes', function(req, res, next){ res.end('Deleteing all dishes'); }) .delete('/dishes/:dishId', function(req, res, next){ res.end('Deleteing the dish '+ req.params.dishId); }); module.exports = dishRouter; 

和server.js:

 var express = require('express'); var morgan = require('morgan'); var bodyParser = require('body-parser'); var host ='localhost'; var port = 3000; var app = express(); app.use(morgan('dev')); app.use(bodyParser.json()); app.use('/dishes', require('./dishRouter')); app.use(express.static(__dirname + '/public')); app.listen(port,host,function(){ console.log(`Server running at http://${host}:${port}`); }); 

在单个文件中,它运行良好,但是当我尝试将它们分离出来,它不起作用,现在我的terminal显示我这个错误:

 Route.all() requires callback functions but got a [object String] 

请问我做错了什么?

更新:30.08.16 @ 23:38

我设法通过不链接.all()到.route()terminal上的错误,所以我现在正在做这个:

 dishRouter.route('/dishes'); dishRouter.all('/dishes', function(req, res, next){ res.writeHead(200, {'Content-Type': 'application/json'}); next(); }) .get('/dishes', function(req, res, next){ res.end('will send all dishes to you'); }) .get('/dishes/:dishId', function(req, res, next){ res.end('will send the dish '+ req.params.dishId + ' to you'); }) // ......... the rest as before.......... 

但是:现在我得到所有方法的404(获取,发布,放置,删除):

  Server running at http://localhost:3000 DELETE /dishes/0 404 219.103 ms - 24 GET /dishes/0 404 22.813 ms - 21 GET /dishes/ 404 1.743 ms - 20 GET / 200 7.699 ms - 130 GET /leaders 404 30.800 ms - 20 GET /leader 404 0.591 ms - 19 PUT /leaders/1 404 1.616 ms - 22 PUT /dishes/1 404 0.595 ms - 21 PUT /dishes/1 404 0.847 ms - 21 GET /dishes/1 404 0.857 ms - 21 GET /dishes 404 1.082 ms - 19 POST /dishes 404 0.679 ms - 20 POST /dishes 404 0.901 ms - 20 GET /dishes 404 2.847 ms - 19 POST /dishes 404 0.671 ms - 20 

任何想法现在怎么了? 谢谢..

更新date:31/08/2016 @ 06:28 am

我设法检索数据,我的错误是使用dishRouter.route('/盘'); dishRouter.all('/盘')…..等等。

现在我正在做dishRouter.route('/盘'); dishRouter.all(/)…和params:dishRouter.get(/:dishId)..等等。

所以这里是我最后的文件dishRouter.js:

  var express = require('express'); var dishRouter = express.Router(); var bodyParser = require('body-parser'); dishRouter.use(bodyParser.json()); dishRouter.route('/dishes'); dishRouter.all('/', function(req, res, next){ res.writeHead(200, {'Content-Type': 'application/json'}); next(); }) .get('/', function(req, res){ res.end('will send all dishes to you'); }) .get('/:dishId', function(req, res){ res.end('will send the dish ('+ req.params.dishId + ') to you'); }) .post('/', function(req, res){ res.end('will add the dish ('+ req.body.name + ') with details (' + req.body.description + 'about the dish)'); }) .put('/:dishId', function(req, res){ res.write('Updating the dish ('+ req.params.dishId+ ')'); res.end(' Updating the dish ('+ req.body.name + ') with details ('+ req.body.description + 'about the dish)'); }) .delete('/', function(req, res){ res.end('Deleteing all dishes'); }) .delete('/:dishId', function(req, res){ res.end('Deleteing the dish ('+ req.params.dishId + ')'); }); module.exports = dishRouter; 

更新:05/09/2016 @ 12:30 pm:

上面的解决scheme工作得很好,但我发现有一个更好的方式来构build路由器文件,我发现,通过教程,我一直在coursera网站上跟随哪些(NodeJS服务器端开发)。

作为下面的答案,最后的文件张贴在所有的利益。

再次感谢大家。

不知道你是否已经尝试过,但res.end()用于快速结束没有任何数据的响应。 您的错误可能是由于使用该错误导致的,并尝试将数据传递给它,就像您正在做的那样。

您可以尝试使用res.send()方法结合res.write和res.end,res.send()方法与res.end()类似,除了可以将数据传回给响应。

在这里阅读更多

根据错误,这与.all()方法有关。 不确定,但是你打破文件分开的问题可能来自于你已经声明嵌套path的事实。

 var dishRouter = express.Router(); dishRouter.use(bodyParser.json()); dishRouter.route('/dishes') .get('/dishes', function(req, res, next){ res.end('will send all dishes to you'); }); module.exports = dishRouter; 

接着

 app.use('/dishes', require('./dishRouter')); 

你已经基本上设置了这些路线在/dishes/dishes ,我得到你不想要的path。

如果你想在/dishespath,保持app.use行,但改变这样的router

 var dishRouter = express.Router(); dishRouter.use(bodyParser.json()); dishRouter.get('/', function(req, res, next){ res.end('will send all dishes to you'); }); module.exports = dishRouter; 

我已经find了解决这个问题的最佳做法,虽然我的问题的上述更新整理了一切,但下面的文件看起来更好,更可读,所以这里是最后的dishRoute.js

 var express = require('express'); var dishRouter = express.Router(); var bodyParser = require('body-parser'); dishRouter.use(bodyParser.json()); dishRouter.route('/') .all(function(req, res, next){ res.writeHead(200, {'Content-Type': 'application/json'}); next(); }) .get(function(req, res){ res.end('will send all dishes to you'); }) .post(function(req, res){ res.end('will add the dish ('+ req.body.name + ') with details (' + req.body.description + 'about the dish)'); }) .put(function(req, res){ res.write('Updating the dish ('+ req.params.dishId+ ')'); res.end(' Updating the dish ('+ req.body.name + ') with details ('+ req.body.description + 'about the dish)'); }) .delete(function(req, res){ res.end('Deleteing all dishes'); }); dishRouter.route('/:dishId') .all(function(req, res, next){ res.writeHead(200, {'Content-Type': 'application/json'}); next(); }) .get(function(req, res){ res.end('will send the dish ('+ req.params.dishId + ') to you'); }) .put(function(req, res){ res.write('Updating the dish ('+ req.params.dishId+ ')'); res.end(' Updating the dish ('+ req.body.name + ') with details ('+ req.body.description + 'about the dish)'); }) .delete(function(req, res){ res.end('Deleteing the dish ('+ req.params.dishId + ')'); }); module.exports = dishRouter; 

注意:server.js文件仍然与上面的问题相同。

谢谢。