camcam
camcam

Reputation: 2625

Next.js app deployment as ssr app with some hosting provider restrictions

I have a shared hosting provider that enables hosting node apps. It has a restriction that the app entry file must be called app.js and it must be in folder /usr/home/username/domains/domain/public_nodeapp. The app is started automatically (probably by something like node app.js) upon first access from the web to the domain.

Is it possible to host a next.js app on such a provider as a server-side rendered app (not as static HTML site produced by next export)?

After running next build which makes a production version of the app, the production version has no app.js file and should be started by next start. I am not sure whether and how it could be tweaked (perhaps some file moving or renaming) to match the restrictions mentioned above.

Upvotes: 1

Views: 334

Answers (2)

ddon-90
ddon-90

Reputation: 2976

Yes you can do it using a custom server eg. express.js

I am doing the same thing for Azure, the only difference is that I need a file called server.js

Example:

// app.js
const express = require('express');
const next = require('next');

const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.all('*', (req, res) => handle(req, res));

  server.listen(port, (err) => {
    if (err) throw err;
    /* eslint no-console: "off" */
    console.log(`> Ready on http://localhost:${port}`);
  });
});

I hope this helps.

Upvotes: 2

felixmosh
felixmosh

Reputation: 35503

You can put a app.js file that is placed in the required place, and then call the function that will be called when you trigger next start from the cli.

That means that this file should require https://github.com/zeit/next.js/blob/canary/packages/next/cli/next-start.ts and invoke nextStart.

// app.js
const startServer = require('next/dist/cli/next-start');

startServer();

Upvotes: 3

Related Questions