我最近开始研究节点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}`);
});
在单个文件中,它工作正常,但是当我尝试像上面那样将它们分开时,它不起作用,现在我的终端向我显示此错误:
Route.all() requires callback functions but got a [object String]
请问我做错了什么?
更新:30.08.16 @ 23:38
我设法通过不将.all()链接到.route()来修复终端上的错误
所以我现在要这样做:
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
知道现在怎么了吗?谢谢..
更新日期:31/08/2016 @ 06:28 am
我设法检索了数据,我的错误是使用dishRouter.route('/ dishes');。 dishRouter.all('/ dishes').....依此类推。
现在我正在做dishRouter.route('/ dishes'); dishRouter.all(/)...,对于参数: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:
上面的解决方案很好用,
但是我发现有一种更好的方法来构造路由器文件,我发现通过教程,我一直跟随Coursera网站上的教程(使用NodeJS进行服务器端开发)。
为了所有人的利益,请在下面将最终文件作为答案答复。
再次感谢大家。
最佳答案
不知道您是否已经尝试过此方法,但是使用res.end()快速结束响应而没有任何数据。您可能会由于使用该错误以及尝试将数据传递给它而引起错误。
您可以尝试使用res.send()方法将res.write和res.end合并,该方法的工作方式与res.end()相似,但可以在响应中传递回数据。
阅读更多here