2016-08-29 4 views
1

私は最近ノード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を使ってサーバーサイド開発と呼ばれていることに気づきました。

病気のすべてのための答えとして、最終的なファイルを投稿してください。

もう一度おねがいします。

+0

@brandonscript、私はur編集を受け入れることができますか?コピーして貼り付けるだけですか? –

+0

私はあなたがそれを承認する必要はありませんが、コードスニペットを自由にクリアすることができるという評判が十分にあります。そこには重複したコードがたくさんあります。 。 – brandonscript

+0

ありがとうございました! –

答えて

0

を変更したい場合は、私は私の上記のアップデートが、この問題を解決するためのベストプラクティスを発見しましたまだserver.jsファイル上記の質問と同じ:質問はすべてを整理但し、下記のファイルは、より良く、より読みやすくなりますので、ここで最終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; 

ノートです。

ありがとうございました。

1

これを試したことがあるのか​​どうかは不明ですが、res.end()を使用すると、データなしで応答をすばやく終了できます。あなたのエラーは、それを使って、データを渡そうとすることと相まって、あなたがやっているときに起こるかもしれません。

res.endと同様の方法で動作するres.send()メソッドを使用してres.writeとres.endを組み合わせることができます。ただし、res.end()メソッドでデータを渡すことができます。応答。

がエラーによるhere

+0

全く同じ結果が得られます! –

+0

ルートファイルからオブジェクトをエクスポートしている可能性があります。この回答を見てみましょう:http://stackoverflow.com/questions/21124909/express-routes-get-requires-callback-functions-but-got-a-object-object –

+0

ありがとうございました。 –

1

続きを読む、それはその.all()方法とは何かを持っています。それについてはわかりませんが、ファイルを分割するという問題は、ネストされたパスを宣言したという事実から来ている可能性があります。

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でそれらのルートのパスを設定しました。

あなたの代わりに、/dishesでパスを持っている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; 
+0

ありがとう、私はあなたに私が投票したので、答えを受け入れることができなかった唯一の理由は、自分の状況にある人々が、私が維持している解決策を知ってほしいということです。 .all()メソッド高く評価。 –

+1

とにかく私の答えに間違いがありました。あなたがそれを理解してうれしいです。あなたは解決策を使って質問を更新するのではなく、ここに答えてください。 –

+0

ありがとうございます。 –