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.
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
"scripts": {
"build": "next build",
"export": "next export",
"serve": "serve out"
},
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.
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).
"it works but it's slow because it compiles source files on demand to server requests."
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With