2016-07-27 6 views
0

私はエクスプレス4でのルーティングに問題があります。私はこの例に従っていましたが、ロードされていません。私はちょうど紡ぎ車を手に入れています。routing node.jsとexpress

エクスプレスバージョン4でのルーティング方法を教えてください。

app.js:

var express = require('express'); 

var http = require('http'); 
var app = express(); 


app.set('views', __dirname + '/views'); 
app.set('view engine', 'ejs'); 


var port = (process.env.PORT || process.env.VCAP_APP_PORT || 5000); 

app.use('/birds', require('./controller/bird')); 

http.createServer(function (req, res) { 
    //res.writeHead(200, {'Content-Type': 'text/plain'}); 
    //res.end('Hello World!\n'); 
}).listen(port); 



console.log('Server running at http://127.0.0.1:'+port); 

bird.js:

var express = require('express'); 
var router = express.Router(); 

// middleware specific to this router 
router.use(function timeLog(req, res, next) { 
    console.log('Time: ', Date.now()); 
    next(); 
}); 
// define the home page route 
router.get('/', function(req, res) { 
    res.send('Birds home page'); 
}); 
// define the about route 
router.get('/about', function(req, res) { 
    res.send('About birds'); 
}); 

module.exports = router; 

答えて

1

あなたはapp.listen()関数を呼び出していません。 http.createServerの代わりに、Express関数を呼び出す必要があります。

基本的にはexampleをご覧ください。

関連するコード:

app.listen(3000, function() { 
    console.log('Example app listening on port 3000!'); 
}); 

編集:slebetmanとして、コメントに書いて、それのためのより一般的な方法は次のとおりです。

http.createServer(app).listen(port, function(){ 
    console.log('now listening on port ' + port); 
}); 
+0

代わりに、彼は 'http.createServer(アプリと呼ばれている可能性が).listen(ポート) '。このより一般的な方法は、httpsをサポートする必要がある場合に 'https.createServer(conf、app).listen(port)'で使用できるので便利です – slebetman