NodeJS:如何获取服务器端口? [英] NodeJS: How to get the server's port?

查看:690
本文介绍了NodeJS:如何获取服务器端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你经常看到创建一个Http服务器的Node的例子hello world代码,开始在一个端口上侦听,然后是以下一行:

  console.log('Server正在监听端口8000'); 

但是理想情况下,您需要改为:

  console.log('服务器正在侦听端口'+ server.port); 

如何检索服务器当前正在侦听的端口,而不将数字存储在变量之前调用 server.listen()



我以前看过这个,但是我找不到Node文档。也许这是特定的东西?

解决方案

Express 4.x答案:



Express 4.x(根据下面的Tien Do的回答),现在将app.listen()作为异步操作,所以listener.address()只会返回app.listen里面的数据)的回调:

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

var listener = app.listen(8888,function(){
console.log('Listening on port'+ listener.address()。port); //侦听端口8888
});

Express 3答案



我想你正在寻找(明确具体?):

  console.log(Express服务器侦听在端口%d,app.address()。port)

你可能已经看到了行),当您从 express 命令创建目录结构时:

  alfred @ alfred-laptop:〜/ node $ express test4 
create:test4
create:test4 / app.js
create:test4 / public / images
create:test4 / public /
创建:test4 / logs
创建:test4 / pids
创建:test4 / public / stylesheets
创建:test4 / public / stylesheets / style.less
创建:test4 / views / partials
创建:test4 / views / layout.jade
创建:test4 / views / index.jade
创建:test4 / test
创建:test4 / test /app.test.js
alfred @ alfred-laptop:〜/ node $ cat test4 / app.js

/ **
* Mod ule依赖。
* /

var express = require('express');

var app = module.exports = express.createServer();

//配置

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());
});

//路由

app.get('/',function(req,res){
res.render('index.jade',{
本地人:{
标题:'Express'
}
});
});

//只听$ node app.js

if(!module.parent){
app.listen(3000);
console.log(Express server listen on port%d,app.address()。port)
}


You often see example hello world code for Node that creates an Http Server, starts listening on a port, then followed by something along the lines of:

console.log('Server is listening on port 8000');

But ideally you'd want this instead:

console.log('Server is listening on port ' + server.port);

How do I retrieve the port the server is currently listening on without storing the number in a variable prior to calling server.listen()?

I've seen this done before but I can't find it in the Node documentation. Maybe it's something specific to express?

解决方案

Express 4.x answer:

Express 4.x (per Tien Do's answer below), now treats app.listen() as an asynchronous operation, so listener.address() will only return data inside of app.listen()'s callback:

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

var listener = app.listen(8888, function(){
    console.log('Listening on port ' + listener.address().port); //Listening on port 8888
});

Express 3 answer:

I think you are looking for this(express specific?):

console.log("Express server listening on port %d", app.address().port)

You might have seen this(bottom line), when you create directory structure from express command:

alfred@alfred-laptop:~/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
alfred@alfred-laptop:~/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)
}

这篇关于NodeJS:如何获取服务器端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆