Node.js / Express.js应用程序仅适用于端口3000 [英] Node.js/Express.js App Only Works on Port 3000

查看:1406
本文介绍了Node.js / Express.js应用程序仅适用于端口3000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node.js / Express.js应用程序在我的服务器上运行,只能工作在端口3000,我想知道为什么。这是我找到的:




  • 不指定端口( app.listen()
  • 在端口3001( app.listen(3001))或未使用的任何其他端口,应用程序运行,但网页不加载。

  • 在端口2999上,应用程序会抛出一个错误,因为其他任何端口正在使用该端口。

  • 在端口3000上,应用程序运行,



我知道Express应用程序默认为端口3000.但奇怪的是,我的应用程序只在我明确使其运行时才运行端口3000( app.listen(3000))。



c> / usr / bin / express

  app.set(\'port \ ',process.env.PORT || 3000); 

这是如上所述:将端口设置为指定的端口,如果没有指定,则设置为3000 。



如何让我的应用程式在不同的港口(例如8080或3001)上运作?





编辑:代码示例(非常简单的节点/快速应用)



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

app.get('/',function(req,res){
res.send('hello world');
});

//只适用于3000,无论我设置环境端口或如何设置app.set('port',[value])中的[value]。
app.listen(3000);


解决方案

app.js:

  http.createServer(app).listen(app.get('port'),
function(){
console.log(Express server listening on port+ app.get('port'));
});

显式硬编码您的代码以使用您想要的端口,例如:

  app.set('port',process.env.PORT || 3000); 

此代码表示将您的端口设置为环境变量 PORT 或如果未定义,则将其设置为文本 3000



或者,使用您的环境设置端口。通过环境设置它用于帮助描述 PRODUCTION 开发以及许多平台即服务使用环境根据其规范设置端口以及内部Express配置。以下设置环境键=值对,然后启动您的应用程序。

  $ PORT = 8080 node app.js 

参考您的代码示例,您需要这样的:

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

//将端口8080设置为默认值或除非环境中另有指定
app.set('port',process.env.PORT || 8080);

app.get('/',function(req,res){
res.send('hello world');
});

//只适用于3000,无论我设置环境端口或如何在app.set('port',[value])中设置
// [value]。
// app.listen(3000);
app.listen(app.get('port'));


I have a Node.js/Express.js app running on my server that only works on port 3000 and I'm trying to figure out why. Here's what I've found:

  • Without specifying a port (app.listen()), the app runs but the web page does not load.
  • On port 3001 (app.listen(3001)) or any other port that is not in use, the app runs but the web page does not load.
  • On port 2999, the app throws an error because something else is using that port.
  • On port 3000, the app runs and the web page loads fine.

I know that Express apps default to port 3000. But strangely, my app only runs when I explicitly make it run on port 3000 (app.listen(3000)).

I found this on line 220 of /usr/bin/express:

app.set(\'port\', process.env.PORT || 3000);

Which is doing as previously stated: setting the port to what is specified or to 3000 if nothing is specified.

How could I make my app work on a different port such as 8080 or 3001?

Thanks!

Edit: Code Sample (Very Simple Node/Express App)

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

app.get('/', function(req, res){
    res.send('hello world'); 
});

// Only works on 3000 regardless of what I set environment port to or how I set [value] in app.set('port', [value]).
app.listen(3000);

解决方案

The following works if you have something like this in your app.js:

http.createServer(app).listen(app.get('port'),
  function(){
    console.log("Express server listening on port " + app.get('port'));
});

Either explicitly hardcode your code to use the port you want, like:

app.set('port', process.env.PORT || 3000);

This code means set your port to the environment variable PORT or if that is undefined then set it to the literal 3000.

Or, use your environment to set the port. Setting it via the environment is used to help delineate between PRODUCTION and DEVELOPMENT and also a lot of Platforms as a Service use the environment to set the port according to their specs as well as internal Express configs. The following sets an environment key=value pair and then launches your app.

$ PORT=8080 node app.js

In reference to your code example, you want something like this:

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

// sets port 8080 to default or unless otherwise specified in the environment
app.set('port', process.env.PORT || 8080);

app.get('/', function(req, res){
    res.send('hello world');
});

// Only works on 3000 regardless of what I set environment port to or how I set
// [value] in app.set('port', [value]).
// app.listen(3000);
app.listen(app.get('port'));

这篇关于Node.js / Express.js应用程序仅适用于端口3000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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