Node.js deployment guide

Deploy a Node.js app from Git on Stack2

Start with a small HTTP server you can test locally, then use Git deployment and a web worker on Stack2. Before deploying, confirm the Node.js runtime and how incoming web requests are routed to your worker.

Make one small app and test it locally

Save this as server.mjs at the repository root. The example needs no packages and reads its listening port from the PORT environment variable. Port 3000 is its local fallback.

import { createServer } from 'node:http';

const port = Number(process.env.PORT ?? '3000');

createServer((request, response) => {
  if (request.url === '/health') {
    response.writeHead(200, { 'Content-Type': 'application/json' });
    response.end(JSON.stringify({ status: 'ok' }));
    return;
  }

  response.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  response.end('Hello from Node.js');
}).listen(port, '0.0.0.0');

Run node server.mjs from the repository root and open http://localhost:3000/health. Look for {"status":"ok"}. Commit the file and push it to the Git repo you’ll deploy. See the Node.js HTTP documentation for how the server works.

Deploy from Git and configure the web worker

  1. Confirm the hosting settings. Check that your Node.js hosting setup supports the runtime you need. Confirm the port the platform routes to and how that port reaches the process.
  2. Deploy the branch. Use Git deployment to deploy the repository and branch with server.mjs. For a private repository, arrange authorised access using your approved deployment method.
  3. Set the startup command. Set the web worker’s working directory to the deployed repository root and its command to node server.mjs. If your setup supplies a port through PORT, the example reads it. If your setup uses a different variable or port setting, match the app to that configuration before starting it.
  4. Start the worker and verify it. Open /health on your configured site URL. Expect HTTP 200 and the same JSON response as the local test. If you’re using your own domain, follow the domain and HTTPS checklist.

The sample was tested locally. Confirm your Stack2 deployment settings before using it on your hosting account.

If the site fails to respond

The worker exits
Check startup output for the working directory, file path and runtime version.
The process runs but the site returns a gateway error
Check that the worker listens on the port the web service routes to. Then confirm the domain is assigned to the correct site.
The sample works but your app fails
Check its dependency installation, build step, environment variables and database connections. Use the deployment handover checklist to record them.

You manage releases and worker restarts. Stack2 provides platform support by ticket, and white glove setup is available as a separate service.

Want a compatibility check before deploying?

Send the runtime, build and startup commands, and any database or worker requirements.

All business guides