繁体   English   中英

使用 Phusion Passenger 的 Next.js 应用程序的启动脚本(使用文件启动生产构建)?

[英]Startup script for a Next.js app using Phusion Passenger (starting the production build using a file)?

通常一个 Next.js 应用程序是使用npm run start在使用npm run build构建它之后启动的。 我无法使用命令启动它,因为我的 web 服务器堆栈(Phusion Passenger)需要启动脚本

Phusion乘客

到目前为止我测试过的内容

  • 启动文件: node_modules/.bin/next :它不起作用(JavaScript 错误),我认为是因为默认行为是在开发模式下启动 Next
  • 此处建议的启动脚本:
const path = require('path');

const nextPath = path.join(__dirname, 'node_modules', '.bin', 'next');

process.argv.length = 1;
process.argv.push(nextPath, 'start');

require(nextPath);

但我从 web 服务器收到错误消息:“从应用程序收到不完整的响应”。 从日志中(应用程序已通过next build正确构建):

App 9526 output: ready - started server on 0.0.0.0:3000, url: http://localhost:3000 App 9526 output: Error: Could not find a production build in the '/var/www/vhosts/XXX/nextjs/ .next' 目录。 在启动生产服务器之前尝试使用“下一个构建”构建您的应用程序。 https://err.sh/vercel/next.js/production-start-no-build-id

尝试使用 next.js 文档( https://nextjs.org/docs/advanced-features/custom-server )提供的官方脚本。

我正在使用node v14npm 6.14.15

// server.js
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  createServer((req, res) => {
    // Be sure to pass `true` as the second argument to `url.parse`.
    // This tells it to parse the query portion of the URL.
    const parsedUrl = parse(req.url, true)
    const { pathname, query } = parsedUrl

    if (pathname === '/a') {
      app.render(req, res, '/a', query)
    } else if (pathname === '/b') {
      app.render(req, res, '/b', query)
    } else {
      handle(req, res, parsedUrl)
    }
  }).listen(3000, (err) => {
    if (err) throw err
    console.log('> Ready on http://localhost:3000')
  })
})

我将pessenager指向server.js文件并使用/opt/plesk/node/14/bin/npm run build --scripts-prepend-node-path构建我的资产

暂无
暂无

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

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