2016-07-27 10 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.createServer (conf, app) .listen (port)'와 함께 사용할 수 있기 때문에보다 일반적인 방법이 유용합니다. https를 지원해야하는 경우 – slebetman