简体   繁体   English

PM2在NextJS应用程序的错误端口上启动

[英]PM2 launching on wrong port for NextJS app

I have package.json scripts as follows: 我有如下的package.json脚本:

"scripts": {
  "build": "NODE_ENV=production next build",
  "startLocal": "NODE_ENV=production node server.js",
  "startServer": "NODE_ENV=production pm2 start server.js",
}

Now, my app uses NextJS with a custom Express server. 现在,我的应用程序将NextJS与自定义Express服务器一起使用。 server.js looks a bit like this: server.js看起来像这样:

const { parse } = require('url');
const express = require('express');
const next = require('next');

const env = process.env.NODE_ENV || 'development';
const port = parseInt(process.env.PORT, 10) || 3000;
const app = next({ dev: env === 'development' });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.get('*', (req, res) => {
    handle(req, res, parse(req.url, true));
  });

  server.listen(port, (err) => {
    if (err) throw err;
    console.info(`> Ready on port ${port}`);
  });
});

To build my app, I first run npm run build . 要构建我的应用程序,我首先运行npm run build Then I launch the app on a specific port number PORT=3030 npm run startLocal . 然后,我在特定端口号PORT=3030 npm run startLocal该应用程序, PORT=3030 npm run startLocal This works as expected. 这按预期工作。 However, when I use the script for a PM2 launch PORT=3030 npm run startServer it doesn't pick up the port number and launches on port 3000. 但是,当我将脚本用于PM2启动PORT=3030 npm run startServer它不会选择端口号并在端口3000上启动。

Why is this? 为什么是这样? Is there something else I need to add to the PM2 command in my package.json script? 我的package.json脚本中还需要向PM2命令添加其他内容吗?

pm2仅了解环境变量NODE_PORT(not PORT),因此我认为您需要使用PORT=3030 NODE_PORT=3030 npm run startServer以便将其传递给next和pm2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM