[英]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)需要啟動腳本。
到目前為止我測試過的內容:
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);
但我從 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 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')
})
})
我將pessenager
指向server.js
文件並使用/opt/plesk/node/14/bin/npm run build --scripts-prepend-node-path
構建我的資產
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.