私は最近ノードJを見始めている、私の質問は急行のルーティングです。Route.all()コールバック関数が必要ですが、[オブジェクト文字列])=> 404エラーを表示
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:
は私がdishRouter.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分
@私は(.routeする)(.ALLチェーン化しないことで、端末でのエラーを修正するために管理) のでI`mは今これをやって:
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..........
しかし:
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
任意のアイデア、今何が間違っている:今、私はすべてのメソッド(、ポストを取得し、置く削除)のために404を取得しますか?私のミスはdishRouter.route( '/食器')を使用していた、私はデータを取得するために管理31/08/2016
午前 6:28 @;:
アップデートに..あなたに感謝dishRouter.all( '/ dishes').....など。
現在、私はdishRouter.route( '/ dishes')を行っています。 dishRouter.all(/)...およびparams:dishRouter.get(/:dishId)..など。
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;
更新:2016年5月9日午後12時30分@:しかし、私は見つけるソリューションは、上記うまく働いた
,, ので、ここで
は私の最終的なファイルdishRouter.jsですルータファイルを構造化するより良い方法があることを知りました。チュートリアルを通して、私はcourseraのウェブサイト上でNodeJSを使ってサーバーサイド開発と呼ばれていることに気づきました。
病気のすべてのための答えとして、最終的なファイルを投稿してください。
もう一度おねがいします。
@brandonscript、私はur編集を受け入れることができますか?コピーして貼り付けるだけですか? –
私はあなたがそれを承認する必要はありませんが、コードスニペットを自由にクリアすることができるという評判が十分にあります。そこには重複したコードがたくさんあります。 。 – brandonscript
ありがとうございました! –