简体   繁体   中英

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 . I can't start it with a command because my web server stack (Phusion Passenger) requires a startup script .

Phusion乘客

What I've tested so far :

  • Startup file: node_modules/.bin/next : it does't work (JavaScript error), I think because the default behavior is to start Next in develop mode
  • A startup script as suggested here :
const 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". From the logs (app has been build correctly with 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. Try building your app with 'next build' before starting the production server. 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 ).

I'm using node v14 and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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