When deploying nextjs app to c-panel
hosting it asks for entry point of the application which default is app.js
. In normal react application it’s totally in control but when using nextjs
it’s not clear which js file is resposible to fire-up the application.
any idea on picking right js file as application entry point?
EDIT:
My hosting provider provided me with following code to setup an express app (which uses next’s request handler) to handle the request:
const express = require('express');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();
const port = 3454;
nextApp.prepare().then(() => {
const app = express();
app.get('*', (req, res) => {
return handle(req, res);
});
app.listen(port, err => {
if (err) throw err;
console.log(`> Ready on localhost:${port}`);
});
});
it works but it’s slow because it compiles source files on demand to server requests.
3
Answers
The best solution for me was using some process manager to bootstrap the application.So i used PM2 to start the application and keep it running.I issued following command in a
c-panel
terminal and now things work well :given
server.js
has following code :I’m surprised to see that cpanel have a feature to start up nodejs application.
what you need to understand about app.js:
App.js contains a web server application (from the code above, your hosting provider suggested you to use ExpressJS – the most being used JS web server application) to serve web files to the browser (similar to Apache).
Do you have package.json file?
Do you know what command cpanel has ran to start your application?
Please check if your NextJS App run on development or production mode.
You just need to export your nextjs application, it would work with
pages — whether there are pages
or
app.js — whether there is an app.js file
just add following scripts
You can first build your project and then export it. then you can serve it to check how would it deploy .
Incase of cPanel just extract the nextjs build folder probably named
out
to your folder like xyz.com.There would be an index.html in the build that is your main file.