Publish a Node.js application

What you get: your Node application running on the domain of one of your sites, with HTTPS, and self-starting if it crashes.

Before starting

You need a site that has already been created, and the application needs two things: a package.json and an input file, for example app.js.

And something you have to decide before you start: the domain name goes to serve the application. If you had a website there, it will no longer be visible. If what you want is to try it out, use a separate site.

Your application has to listen on the port that we passed it in the PORT environment variable. If you have it written in fire, it will not respond.

Steps

  1. Upload the files to the site folder, with the File manager or by SFTP (Upload your files: FTP, SFTP and the file manager).

  2. Websites → Node.js apps → New application menu.

  3. Fill in:

    • Site - your domain will be used to serve this application.

    • Name — to recognize it in the list.

    • Entry file - the one that starts the application, inside its folder. For example app.js.

    • Folder (optional) — inside the site folder. Empty, the root.

    • Environment variables (optional) — one per line, with the shape CLAVE=valor.

  4. Create application.

When you start it, we install the dependencies (npm install --production) and give you its port in PORT.

How to check it worked

The application queue must remain running, and https://tudominio.com must return what your application returns, not the previous website.

If it doesn’t appear, open the Application log from its row: that’s what Node wrote when it started, which is where the real error is seen.

The Start, Restart and Stop buttons are in the same row.

Common problems

The application starts and the domain gives error 502 or does not respond

It’s almost always the port. You have to listen in process.env.PORT, not in a entered manually number. Look at it in the registry: if it says it listens on 3000 and we gave it another one, that’s it.

The log says “Cannot find module”

There is a dependency missing in the package.json, or you installed it as a development dependency. Since we install with –production, the development ones do not enter: change it to the normal ones and restart.

Upload the files and you cannot find the entry file

The entry file path is relative to the application folder, not the site root. If your app.js is inside a subfolder, put it in “Folder”.

I have uploaded changes and it still works as before

Node does not load the code when starting. Press “Restart” in its row and look again.

My previous website has disappeared

That’s what it does: the domain name passes to serve the application. Stop the application and the site will serve its files again.

I need a database for the application

The databases of your sites are MariaDB and they are right there. If you want PostgreSQL or Redis, they are ordered separately: see Managed databases for Projects and VPS.

Can I have two applications on the same domain?

No: one domain serves one. Use another site, or a subdomain with its own site.

The application crashes every now and then

We restart it automatically each time, so the service stays available, but the problem is in your code. The log records what happened before each crash: that’s where the clue is.