Create a Node.js server for app hosting

2 min read07 November 2026

A Node.js deployment rarely fails because of the language: it fails because the main file cannot be found, a dependency is missing or the server listens on an unreachable address. Start from a minimal server with no dependencies, confirm it runs locally, then add your logic. You will then be able to tell a platform problem from a code problem.

Minimal structure

Two files are enough: the entry point and the manifest.

Text
my-app/
  index.js
  package.json

The HTTP server

The code below uses only the standard library. It reads the port from the environment, listens on every interface and exposes a health route.

JavaScript
const http = require("node:http");

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

const server = http.createServer((req, res) => {
  if (req.url === "/health") {
    res.writeHead(200, { "content-type": "application/json" });
    res.end(JSON.stringify({ status: "ok" }));
    return;
  }
  res.writeHead(200, { "content-type": "text/plain; charset=utf-8" });
  res.end("Hello from Node.js\n");
});

server.listen(port, "0.0.0.0", () => {
  console.log(`listening on ${port}`);
});

process.on("SIGTERM", () => server.close(() => process.exit(0)));

0.0.0.0 is essential: with 127.0.0.1 the server accepts only local connections and stays unreachable from outside. The 3000 fallback is for local development only; on the platform, PORT is provided.

The package.json

JSON
{
  "name": "my-app",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "engines": {
    "node": ">=20"
  }
}

main and the start script must point to the real file. If you rename index.js to server.js, update all three: the file name, main and the start command.

Deploy

  1. Upload the files or clone the repository into the service.
  2. Run npm install if a package.json declares dependencies.
  3. Confirm the start command is node index.js (or npm start).
  4. Start, then read the console: the listening on ... line must appear.
  5. Call /health from outside to confirm the port is exposed.

Do not upload node_modules: the platform installs declared dependencies. However, always commit package-lock.json for reproducible installs.

Common errors

Message Cause Fix
Cannot find module missing dependency or wrong path npm install, check main
EADDRINUSE two processes on the same port one process, use the assigned port
Site unreachable listening on 127.0.0.1 listen on 0.0.0.0
PORT undefined variable read incorrectly read process.env.PORT
Restart loop exception at startup read the console, fix the error

A minimal server that answers on /health is the best diagnostic base: as long as it works, the problem is in your code, not the platform.

Advertisement

Was this article helpful?

Read next