快速4.x的答案:
快遞4.x版(每下面田待辦事項的回答),現在把app.listen()作爲一個異步操作,所以listener.address()纔會返回數據裏面app.listen的()的回調:
var app = require('express')();
var listener = app.listen(8888, function(){
console.log('Listening on port ' + listener.address().port); //Listening on port 8888
});
快3回答:
我認爲你正在尋找這個(表達具體的?):
console.log("Express server listening on port %d", app.address().port)
你可能已經看到了這(底線),當您從express
命令創建的目錄結構:
[email protected]:~/node$ express test4
create : test4
create : test4/app.js
create : test4/public/images
create : test4/public/javascripts
create : test4/logs
create : test4/pids
create : test4/public/stylesheets
create : test4/public/stylesheets/style.less
create : test4/views/partials
create : test4/views/layout.jade
create : test4/views/index.jade
create : test4/test
create : test4/test/app.test.js
[email protected]:~/node$ cat test4/app.js
/**
* Module dependencies.
*/
var express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.bodyDecoder());
app.use(express.methodOverride());
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.render('index.jade', {
locals: {
title: 'Express'
}
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port)
}
查找`REQ。 headers.host` – 2016-01-09 13:14:00