[英]Startup script for a Next.js app using Phusion Passenger (starting the production build using a file)?
Usually a Next.js app is started using npm run start
after building it with npm run build
.通常一个 Next.js 应用程序是使用npm run start
在使用npm run build
构建它之后启动的。 I can't start it with a command because my web server stack (Phusion Passenger) requires a startup script .我无法使用命令启动它,因为我的 web 服务器堆栈(Phusion Passenger)需要启动脚本。
What I've tested so far :到目前为止我测试过的内容:
node_modules/.bin/next
: it does't work (JavaScript error), I think because the default behavior is to start Next in develop mode启动文件: node_modules/.bin/next
:它不起作用(JavaScript 错误),我认为是因为默认行为是在开发模式下启动 Nextconst path = require('path');
const nextPath = path.join(__dirname, 'node_modules', '.bin', 'next');
process.argv.length = 1;
process.argv.push(nextPath, 'start');
require(nextPath);
But I receive an error from the web server: "Incomplete response received from application".但我从 web 服务器收到错误消息:“从应用程序收到不完整的响应”。 From the logs (app has been build correctly with next build
):从日志中(应用程序已通过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' directory. 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' 目录。 Try building your app with 'next build' before starting the production server.在启动生产服务器之前尝试使用“下一个构建”构建您的应用程序。 https://err.sh/vercel/next.js/production-start-no-build-id https://err.sh/vercel/next.js/production-start-no-build-id
Try using the offical script provided by next.js docs ( https://nextjs.org/docs/advanced-features/custom-server ).尝试使用 next.js 文档( https://nextjs.org/docs/advanced-features/custom-server )提供的官方脚本。
I'm using node v14
and npm 6.14.15
我正在使用node v14
和npm 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')
})
})
I pointed pessenager
to the server.js
file and build my assets with /opt/plesk/node/14/bin/npm run build --scripts-prepend-node-path
我将pessenager
指向server.js
文件并使用/opt/plesk/node/14/bin/npm run build --scripts-prepend-node-path
构建我的资产
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.