How to Create a Node.js Application
Create, deploy and restart a Node.js application from the Node.js Selector in the web hosting control panel on CR Servers shared hosting.
The Node.js Selector runs a Node.js application on your domain, or on a path of it such as https://yourdomain.com/app/. The server starts your app and forwards web traffic to it, so you don't open or configure a public port.
Two ideas to keep apart:
- Application root is the folder in your hosting account that holds the code, for example
apps/my-node. - Application URL is the public address visitors open, for example
https://yourdomain.com/app/.
Keep the application root outside your website's public folder so your source code and settings can't be downloaded.
Prepare your app
- Your project needs a
package.jsonthat lists its dependencies, and a startup file such asapp.js. - Have the app listen on the port it receives in
process.env.PORT, for exampleapp.listen(process.env.PORT || 3000). - Leave
node_modulesout of the upload: the selector installs dependencies for you.
Create the application
- In the web hosting control panel, open CloudLinux → Node.js Selector (the cloud icon in the left menu).
- Click Create Application.
- Fill in the form:
- Node.js version: the list starts on an old version. Change it to a current one your app supports; the server marks one as (recommended).
- Application mode: the form starts on Development. Choose Production for a live site.
- Application root: the folder for the code, inside your home folder, for example
apps/my-node. - Application URL: pick the domain from the drop-down (it can show a placeholder label such as
[object Object]until you choose one), then type a path in the box next to it, for exampleapp. - Application startup file: the file that starts your app, for example
app.js.
- Add any settings your app reads (API URLs, keys) with Add Variable under Environment variables instead of putting them in files.
- Click Create.
If you leave the path empty, the app answers for the whole domain instead of the files in its public folder. Use a path such as app if you want to keep your current site.
Upload code and install dependencies
- Upload your project into the application root with the file manager or FTP. The startup file and
package.jsongo at the top of that folder. - In the Node.js Selector, click the pencil icon next to the application to open it.
- Under Detected configuration files, click Run NPM Install.
- Click Restart.
- Click Open next to the application URL and check that your app responds over HTTPS.
After each code change, upload the files and restart the app, either with Restart inside the app or the circular-arrow icon in the list. You don't need to restart anything else. If you change a setting in the form, click Save.
Stop, start or delete
The Web Applications list shows each app's Status and Node.js version, with Actions icons to stop, restart, edit and delete it. The same controls are inside the app.
- Stop App (square icon in the list) takes the app offline until you start it again. Your files stay in place.
- Destroy (trash icon in the list) removes the application from the selector. It isn't the same as stopping it. Download a copy of your code first, and contact support if you're not sure what will be kept.
If it doesn't work
- Error page or blank response: check that the startup file name matches the file you uploaded, then restart the app.
- Missing module errors: check that
package.jsonlists the module, run Run NPM Install again, then restart the app. - Changes don't show up: restart the app after every upload.
- HTTPS or domain errors: check that the domain points to your hosting and has SSL active (SSL guide).
If the selector is missing or the app still won't start, open a ticket with the domain, Node.js version, failing URL, approximate time and the error message (remove passwords and keys). For heavy apps, background workers or WebSockets, consider a VPS.