【问题标题】:how to set local node ports for production?如何为生产设置本地节点端口?
【发布时间】:2018-07-09 17:13:32
【问题描述】:

试图将本地项目带到 ubuntu mongodb 服务器上。 目前,当我在服务器上运行npm start 并访问curl http://localhost:8000 时,该项目在我的localhost:8000 上运行,我可以看到我的主页的标记正在输出。如何更改此设置以在生产中使用我的域/服务器 ip?

下面是我的节点文件,由npm start运行

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('05-express-first-app:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '8000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

我的服务器ip地址是:165.227.196.209,只是不知道放在哪里。

谢谢!

【问题讨论】:

  • 你真的在你的服务器上运行代码吗?
  • 只需在服务器上使用 localhost 设置运行服务器并尝试从浏览器中点击。
  • 你能详细说明一下吗?
  • 你需要把代码放到你的服务器上,然后在你的服务器上运行npm start
  • 我已经做到了。当我运行npm start 时,服务器正常启动,我可以访问curl http://localhost:8000 并查看来自我的服务器的获取请求。但是在网络浏览器中,我无法查看我的网站,并且它没有被我的域击中

标签: node.js mongodb ubuntu


【解决方案1】:

您必须将运行 Node.js 服务器的 IP 更改为服务器的公共 IP 地址。在您的情况下,公共 IP 是 165.227.196.209

这是假设您的名称服务器已配置为将域(例如:mywebsite.com)路由到该特定 IP 地址。

在此处阅读更多相关信息 - How to assign a domain name to node.js server?(检查所选答案)

更新:: 打开 URL - http://165.227.196.209/, 我注意到您正在使用 nginx。所以你必须设置 reverse proxying

我建议通过这里的链接 - Node.js + Nginx - What now?

您可以在此处阅读有关将 nginx 信息放入 Node.js 的好处 - Using Node.js only vs. using Node.js with Apache/Nginx

我在这里注意到的其他几个问题可以很好地解决,

  1. 您应该使用流程管理器在生产环境中运行您的应用程序。推荐使用pm2 之类的东西。这样,如果您的应用程序崩溃,pm2 将重新启动它。
  2. 您可以将 pm2 与此处所述的环境变量一起使用,以便在生产环境的不同端口上运行 - http://pm2.keymetrics.io/docs/usage/environment/

【讨论】:

  • 一个人应该可以只使用IP地址而不用担心域名。它也应该在没有 PM2 的情况下工作。我认为问题出在其他地方。
  • @Aᴍɪʀ - 更新,他在 nginx 后面运行服务器,所以他必须设置一个反向代理。
  • 但是,nginx 只使用 80 端口!他的节点应用默认使用8000端口,那里也不应该有问题!
  • @Aᴍɪʀ - 看看他的评论,他说“我已经做到了。当我运行 npm start 时,服务器正常启动,我可以访问 curl localhost:8000 并查看来自我的服务器的获取请求. 但是,在网络浏览器中,我无法查看我的网站,并且它没有被我的域击中”。进入他的域显然会路由到 nginx,它对后台运行的 Node.js 服务器一无所知。
  • 我认为他还缺少其他东西! :耸肩:
猜你喜欢
  • 2019-04-03
  • 2022-01-23
  • 2019-02-24
  • 2012-11-11
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
相关资源
最近更新 更多