Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is entry point of nextjs application?

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.

enter image description here

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.

like image 514
Behnam Esmaili Avatar asked Oct 15 '22 06:10

Behnam Esmaili


2 Answers

You just need to export your nextjs application, it would work with

pages -- whether there are pages

  • index.js
  • example.js

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 outto your folder like xyz.com.

There would be an index.html in the build that is your main file.

like image 64
Zunaib Imtiaz Avatar answered Oct 21 '22 00:10

Zunaib Imtiaz


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.

like image 21
Darryl RN Avatar answered Oct 20 '22 23:10

Darryl RN