Skip to content
Deploying apps4 min read

How to deploy a Node.js backend or API

Short answer

Put the API code in a folder outside public_html, create an app for it in Setup Node.js App on a subdomain such as api.example.com, and make the code listen on process.env.PORT. Add secrets under Environment variables, allow your front end's origin in CORS, and test a /health endpoint with curl before connecting the front end.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Before you start
  2. Steps
  3. Troubleshooting
  4. On ArabyHost

The API is the part visitors never see: it takes requests from your front end or mobile app, reads the database and answers in JSON. Deploying it is like deploying any Node.js app, with four things specific to APIs: a stable address such as api.example.com, CORS, secrets in environment variables, and a health endpoint to prove it works. The field-by-field tour of Setup Node.js App is in the Node.js app guide.

Before you start

Steps

1. Make the code production-ready

Open the file that starts the server (usually server.js, index.js or app.js) and check three things: the port comes from process.env.PORT, CORS allows only your front end, and there’s a simple health route:

const express = require('express');
const cors = require('cors');

const app = express();
app.set('trust proxy', 1);           // the app runs behind the web server
app.use(express.json({ limit: '1mb' }));
app.use(cors({ origin: process.env.CORS_ORIGIN?.split(',') }));

app.get('/health', (req, res) => res.json({ ok: true }));

app.get('/api/products', async (req, res) => {
  res.json([{ id: 1, name: 'Example' }]);
});

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`API listening on ${port}`));

trust proxy matters because requests reach your app through the web server; without it, rate-limiting libraries see every visitor as one address. For TypeScript, build first (npm run build) and use the output as the startup file, for example dist/server.js.

2. Create the API subdomain

In cPanel, open Domains, click Create A New Domain and enter api.example.com. A separate host keeps the API apart from the front end and makes certificates and logs clearer. AutoSSL issues its certificate automatically once the domain points to the hosting. If the front end and API live in one project, see deploy a full stack app for the /api path option.

3. Upload the code outside public_html

Put the project in a folder such as /home/USER/api, from GitHub through Git Version Control (see push a project to GitHub) or as a ZIP through File Manager. Don’t upload node_modules or .env.

4. Create the app in Setup Node.js App

Click Create Application and fill in:

Field Value
Node.js version 20 or 24, to match your project
Application mode Production
Application root api
Application URL api.example.com
Application startup file server.js or dist/server.js

Click Create, then Run NPM Install to install packages into the virtual environment.

5. Add environment variables

On the app’s page, under Environment variables, add every value your local .env had, for example:

Name Example
DATABASE_URL mysql://cpuser_api:[email protected]:3306/cpuser_api
JWT_SECRET A long random string from the password generator
CORS_ORIGIN https://example.com,https://www.example.com

Click Save, then Restart. If your code uses dotenv that’s fine, but values set on this screen are already in process.env. More in setting .env variables on hosting.

6. Create the tables

With Prisma, Sequelize or Knex, run migrations over SSH inside the virtual environment, for example npx prisma migrate deploy. Without SSH, export the tables from your computer as an SQL file and import it in phpMyAdmin. Prisma notes are in Prisma on hosting.

7. Test the API before connecting the front end

From your computer:

curl -i https://api.example.com/health
curl -i -H "Origin: https://example.com" https://api.example.com/api/products

Success is HTTP/2 200 with {"ok":true} for the first, and an access-control-allow-origin: https://example.com header in the second response. Only then put the API URL into your front end’s settings.

8. Later updates

After pulling new code, click Run NPM Install if package.json changed, then Restart, or over SSH:

cd ~/api && touch tmp/restart.txt

Troubleshooting

  • 503 or “Web application could not be started”: the app crashed on startup; the reason is in the Passenger log. See Node.js app 503.
  • CORS error in the browser: CORS_ORIGIN doesn’t exactly match the front end’s origin (scheme, and with or without www). See CORS error.
  • ECONNREFUSED ::1:3306: localhost resolved to IPv6. Use 127.0.0.1. Details in database connection refused after deploy.
  • Cannot find module: the package is in devDependencies or wasn’t installed. See Cannot find module.
  • The first request after a quiet period is slow: Passenger may stop an idle app and start it on the next request. Keep startup light.

On ArabyHost

Node.js APIs run on ArabyHost AI/Developer plans, with Node.js 20 and 24 through the CloudLinux selector and Phusion Passenger, SSH jailed in CageFS, Git, MySQL/MariaDB on the same account, and a free SSL certificate for the subdomain. Each account has its own resource limits (LVE), and there’s no Docker, root or GPU. If the log isn’t clear, the Araby AI assistant reads the error log and explains the fix. More on the developers page.

FAQ

Can I deploy a Node.js backend for free?

Some platforms have free tiers, but most put the app to sleep after a period without requests or cap hours and database size, so check their official pages. A real project needs hosting that runs Node.js with a database on the same account. ArabyHost has no free plan.

Do I need PM2 to run the API?

No. On cPanel hosting with CloudLinux, Phusion Passenger starts the app when requests arrive and restarts it if it crashes. PM2 is useful on a VPS where you control the whole server.

Do NestJS and Fastify work the same way?

In principle, yes: build the project to JavaScript, set the startup file to the output (such as dist/main.js), and listen on process.env.PORT. Try it on the hosting early, because some advanced framework settings need small adjustments under Passenger.

Why does the API work in Postman but not from the browser?

Usually CORS. Postman doesn't enforce CORS, but the browser rejects the response unless it carries an Access-Control-Allow-Origin header with your front end's origin.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.