Starting the Node.js application in the control panel
Go to your control panel HOSTING 2.0
In the menu, select the section for running Node.js applications and choose one of the supported versions. If the functionality was not activated before, the system will offer you to choose the version of Node.js on which your project will work.
At this point, we recommend that you refer to your application's documentation to select the correct version. Select the desired version and click Save.
If you did not find the version you need in the list, please contact our technical support at support@cityhost.net.ua from the registration mail or from control panel.
Run the stub application.
Installing Next.js
To install Next.js, use the official guide: Next.js Installation Guide.
Connect to your server via SSH. In the terminal, navigate to the website directory:
cd www/
Creating a new Next.js application
Run the command to create a new Next.js application using npx:
npx create-next-app@latest yoursite.com
In this example yoursite.com — must be replaced with the name of your website.
After executing the command, wait for the application creation process to complete. This command will create all the necessary files and directories for the Next.js application to work.
Creating the server.js file
After the application is created, you need to create a server.js file that will be responsible for running the application through the HTTP server.
Create a new server.js file in the root of your application and add the following code to it:
const next = require('next');
const fs = require('fs');
const http = require('http');
const socketPath = process.env.PORT;
const app = next({ dev: true });
const handle = app.getRequestHandler();
app.prepare().then(() => {
if (fs.existsSync(socketPath)) {
fs.unlinkSync(socketPath);
}
const server = http.createServer((req, res) => {
handle(req, res);
});
server.listen(socketPath, () => {
console.log(`Server is listening on ${socketPath}`);
});
fs.chmodSync(socketPath, '0777');
});
This file will be used to launch the application on your server using the HTTP protocol over a socket.
Setting up the package.json file 1
Open the package.json file of your application, find the scripts section and change the command to start the application: Change: "start": "next start", to:
"start": "node ./server.js",
Assembly of application for production environment
If you plan to deploy the application to a production environment, you must first build the application. To do this, execute the command:
npm run build
This command will collect all static files and optimize the application for production environment.
After completing the build or settings, you can start the service through the control panel:
If everything is configured correctly, the application will launch, and you can open it in the browser. Now you can enjoy a working Next.js application on your server!
All question categories