This article describes how to migrate an existing Node.js application to the Node.js Selector in cPanel.
In most cases, Node.js applications can be migrated to Node.js Selector with only minor modifications. To do this, follow these steps:
const express = require('express') const app = express() const port = 3000 app.get('/myapp', (req, res) => res.send('Hello World!')) app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
This code can be modified to obtain a port number dynamically as follows:
const express = require('express') const app = express() app.get('/myapp', (req, res) => res.send('Hello World!')) const server = app.listen(0, () => { console.log('Example app listening at http://localhost:', server.address().port); });
The parameter 0 in the app.listen() statement instructs Node.js to obtain a port number dynamically.
Import directives: Some Node.js versions do not support import directives. Therefore, you should modify any import statements to use require instead. For example, consider the following statements:
import express from "express"; import path from "path"; import dotenv from "dotenv";
These statements can be modified to work correctly as follows:
const express = require('express'); const path = require('path'); const dotenv = require('dotenv');
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.