Node.js: Quickstart guide

Deploy Node.js on Stackhero quickly and securely

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Node.js cloud solution designed to help you move faster:

  • Deploy to production in seconds with a simple git push. No additional tools or manual configuration needed.
  • Use your own domain and take advantage of automatic HTTPS certificate setup, keeping your application secure with no extra steps.
  • Enjoy automatic backups, one-click updates, and predictable, transparent pricing so you can focus on building features while Stackhero manages the infrastructure.
  • Benefit from dedicated, private infrastructure for optimal performance and enhanced security.

Save time and simplify your operations: you can have your application running on Stackhero's Node.js cloud hosting in just a few minutes.

Deploying your Node.js service on Stackhero is designed to be fast, efficient, and reliable. This guide shows you how to get your application online in just a few minutes, with security and performance built in from the start.

Start by creating a Node.js service on Stackhero. This provides your application with a secure cloud environment and access to Stackhero's automated features.

Before you begin, make sure you have the following:

  1. Git: Download it from the official Git website.
  2. Windows users: For a smoother terminal experience, you can use Windows Terminal from the Microsoft Store.

The key configuration step is to add your SSH public key. This allows Stackhero to securely access your repository for deployments.

To display your public key, run one of these commands in your terminal:

cat ~/.ssh/id_rsa.pub

or

cat ~/.ssh/id_ed25519.pub

If you do not have an SSH key pair, you can generate one with ssh-keygen on Linux or macOS, or ssh-keygen.exe on Windows.

Once you have your public key, log in to the Stackhero dashboard, select your Node.js service, open the configuration section, and paste your key into the provided field.

Tip: To use your SSH public key for all future services, you can set it globally. In the Stackhero dashboard, click your profile picture (top right), go to "Your profile", and paste your SSH public key there.

To make your first deployment even easier, Stackhero provides a sample Node.js app. You can clone it with:

git clone https://github.com/stackhero-io/nodejsGettingStarted.git stackhero-nodejs-getting-started
cd stackhero-nodejs-getting-started

You can deploy your app via Git. On your service's main page, Stackhero provides a command to add a Git remote, similar to:

git remote add stackhero ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git

You can copy and paste this command into your terminal to set up the remote.

To deploy, push your code to Stackhero:

git push stackhero main

The first time you push, confirm the key fingerprint by typing "yes" if prompted.

Within seconds, your application is live. You can check its status by visiting the website URL shown on your Stackhero dashboard (for example, https://<XXXXXX>.stackhero-network.com).

Your application is now deployed.

To update your app, modify your app.js file, commit, and push:

git add -A .
git commit -m "Update app.js"
git push stackhero main

If you already have a Node.js app, you can add the Stackhero remote and deploy your code:

git push stackhero main

Stackhero reads the Node.js version to use from your repository at each deployment. The version is determined in this order:

  1. image in your stackhero.yml, for example: image: node:24-alpine3.21
  2. engines.node in your package.json, for example: "node": "24.x"
  3. .nvmrc
  4. .node-version

If none of these are present, Stackhero uses the current Long Term Support (LTS) release. Using a range like 24.x ensures you always get the latest updates for that major version.

Full details on version pinning, available tags, and update behaviour are in Node.js version.

If you see this error:

error: failed to push some refs to '[...]'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
(e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Your local repository is out of sync with the remote. You can force the push:

git push -f stackhero main

If you see this:

error: src refspec main does not match any
error: failed to push some refs to 'ssh://<XXXXXX>.stackhero-network.com:222/project.git'

This usually means the main branch does not exist. You can try pushing the master branch:

git push stackhero master

If Git says "Everything up-to-date" but your changes are not deployed, you may have forgotten to commit your changes. Try:

git add -A .
git commit -m "Your commit message"
git push stackhero main

To trigger a deployment without any changes, you can force an empty commit:

git commit --allow-empty -m "Force update"
git push stackhero main

To deploy a branch other than main, such as production, you can run:

git push stackhero production:main

To deploy a specific tag (for example, v1.0), you can use:

git push stackhero 'v1.0^{}:main'

The ^{} ensures the commit referenced by the tag is pushed.

To deploy a specific commit, first get its hash with git log, then:

git push -f stackhero <HASH>:main

To manage separate environments, such as production and staging, you can rename the current remote to stackhero-staging:

git remote rename stackhero stackhero-staging

Then create a new Node.js service in the dashboard and add it as stackhero-production:

git remote add stackhero-production ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git

Now you can deploy to each environment with:

git push stackhero-production main

or

git push stackhero-staging main

On macOS, if you are prompted for your SSH key password on every push, you can use the macOS keychain for added convenience and security. For example:

/usr/bin/ssh-add --apple-use-keychain ~/.ssh/id_rsa

This command saves your key password so you will not be prompted again during future deployments.

When working with multiple environments, it is safest to use environment variables for secrets such as tokens or passwords. This keeps sensitive data out of your repository.

You can manage environment variables in the Stackhero dashboard. Access them in your Node.js code like this:

process.env.mySecret

If your application requires protocols other than HTTP, you can open additional TCP or UDP ports directly in the Stackhero dashboard. Just specify the public entry port, the destination port on your Node.js service, and the protocol.

For user files such as photos, it is recommended to use object storage. This approach lets you share files between services and instances, and keeps your data separate from your code. MinIO is a high-performance, S3-compatible solution.

If you prefer local storage, you can use the persistent storage located at /persistent/storage/ on your Node.js instance.

CAUTION: Always store your data inside the /persistent/storage/ folder.

Data stored outside this folder may be lost if your instance reboots or when you push new code.

When you deploy a new version, the previous application receives a termination signal so it can shut down cleanly. This allows your app to close database connections or other resources.

The SIGTERM signal is sent to your application. You can handle it in your code:

process.on('SIGTERM', () => {
  // This log appears in the Stackhero dashboard logs tab
  console.info('SIGTERM signal received.');

  // Close database connections and other services here
  // ...
});

By default, Node.js uses a single core. To take advantage of all available CPU cores, you can use the Node.js cluster API. The official Node.js cluster documentation explains this in detail.

Here is a simple example:

const cluster = require('cluster');
const http = require('http');
const cpusCount = require('os').cpus().length;

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork workers
  for (let i = 0; i < cpusCount; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
  });
} else {
  // Workers share any TCP connection, in this case, an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);

  console.log(`Worker ${process.pid} started`);
}