Skip to main content

Managing and deploying Node.js applications

Starting a Node.js application

To start a Node.js application from the mStudio, follow these steps:

  1. Navigate to the project that you want to create the application in.
  2. Select the "Apps" menu item in the sidebar.
  3. Click the "Create App" button and select "Custom Node.js app".
  4. In the installation wizard, select the entrypoint command that should be used to start your application and complete the wizard.

After the installation has completed, observe the installation directory in the UI.

Deploying your app

After you have created your Node.js app, you can deploy your code by moving it into the designated application directory. You may use any method that you prefer to deploy your code, such as a local Git clone, rsync or SFTP.

Have a look at our collection of deployment guides for more information on how to deploy your code.

Making your app accessible via HTTP

See our guide on Running Webservers to learn how to make your Node.js app accessible via HTTP.

Managing your process lifecycle

See our guide on Running Webservers to learn how to manage the process lifecycle of your Node.js application.

Example applications

Express.js

The following code snippet shows a minimal example of an Express application that listens on the port specified in the PORT environment variable:

app.js
const express = require("express");
const app = express();
const port = parseInt(process.env.PORT, 10) || 3000;

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.listen(port, () => {
console.log(`Server running on port ${port}`);
});

PM2 with live-reloading

To enable advanced features like live-reloading (useful for development) or clustering (when you want to use all available CPU cores), you can use the PM2 process manager.

For this, you need to create a Node.js app with pm2 as its entrypoint (or alternatively, configure pm2 start ecosystem.config.js --no-daemon as the start script in your package.json file):

$ mw app create node --wait --entrypoint "pm2 start ecosystem.config.js --no-daemon"

Furthermore, you need to have PM2 installed in your application environment. You can install it via NPM:

SSH shell session
$ npm install -g pm2

Next, create an ecosystem configuration file ecosystem.config.js in your application directory:

ecosystem.config.js
module.exports = {
apps: [
{
name: "test-app",
script: "src/server.js",
watch: ["src"],
},
],
};

In this example, PM2 will start the src/server.js script as the main application, and watch the src directory for changes. If any changes are detected, PM2 will automatically restart the application. Have a look at the PM2 documentation reference for more information.

Using Yarn Plug'n'Play

If your project uses the Yarn PnP installation mode, it is recommended to disable the global cache.

.yarnrc
enableGlobalCache: false

Environment Variables

Environment variables are generally used to configure Node.js applications. Depending on the Node.js version, we recommend different variants to define these.

You can define environment variables in the file ~/.config/node/.env. Variables defined there are then automatically available in your application.