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

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

问题描述

您经常会看到 Node 的示例 hello world 代码,它创建了一个 Http 服务器,开始侦听端口,然后是类似以下内容的代码:

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

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

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

我以前见过这样做过,但在 Node 文档中找不到.可能是为了表达什么?

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 答案:

Express 4.x(根据下面的 Tien Do 的回答),现在将 app.listen() 视为异步操作,因此 listener.address() 将仅返回 app.listen() 回调内的数据:

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

表达 3 个答案:

我认为您正在寻找这个(具体表达?):

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

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

当您从 express 命令创建目录结构时,您可能已经看到了这一点(底线):

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天全站免登陆