NavigationContentFooter
Jump toSuggest an edit

Running a Node.js + Express Server on Serverless Container

Reviewed on 15 July 2024Published on 14 October 2021
  • serverless
  • containers
  • caas
  • nodejs
  • express-server

Express is a lightweight Node.js framework designed to ease building applications and APIs.

In this tutorial you will learn how to configure your Express.js application to run on Scaleway Serverless Container.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • A Serverless Containers namespace
  • Installed Node.js, NPM, and Docker on your computer

Creating an Express.js application

To begin, we need to create a web server with Node.js and an Express.js framework.

  1. Create a new folder for the project:

    mkdir my_nodejs_project
  2. Install the express library:

    npm install express

    Two files are created during this step: package.json and package-lock.json.

  3. Create a server.js file with the following code. This creates a simple Web Server using Express.js:

    const express = require('express');
    // Constants
    const PORT = process.env.PORT || 8080;
    const HOST = '0.0.0.0';
    // App
    const app = express();
    app.get('/', (req, res) => {
    res.send('Hello World');
    });
    app.listen(PORT, HOST);
    console.log(`Running on http://${HOST}:${PORT}`);

Configuring the Docker image

To Dockerize our simple web app, we will use the official Node.js image.

  1. Create a Dockerfile using the following command:

    touch Dockerfile
    Tip

    A Dockerfile is a document that contains all the commands a user could call on the command line to create an image.

  2. Open the Dockerfile in a text editor and specify the following actions:

    • Define the node version you want to use:
    FROM node:14
    • Create a directory to store your application code:
    # Create app directory
    WORKDIR /usr/src/app
    • Install the application’s dependencies using the npm binary:
    # Install app dependencies
    # A wildcard is used to ensure both package.json AND package-lock.json are copied
    COPY package*.json ./
    RUN npm install
    • Copy your code inside the Docker image:
    # Bundle app source
    COPY . .
    • Your app binds to the port defined in the environment variable PORT. Define it and expose the port 8080:
    ENV PORT 8080
    EXPOSE 8080
    • Use CMD to run the node server.js command that will start your application:
    CMD [ "node", "server.js" ]

    The final Dockerfile should look like this example:

    FROM node:14
    # Create app directory
    WORKDIR /usr/src/app
    # Install app dependencies
    # A wildcard is used to ensure both package.json AND package-lock.json are copied
    COPY package*.json ./
    RUN npm install
    # Bundle app source
    COPY . .
    ENV PORT 8080
    EXPOSE 8080
    CMD [ "node", "server.js" ]

    To lighten your image, some folders and files need to be excluded from the image generation:

  3. Create a .dockerignore file in the same directory as your Dockerfile:

    touch .dockerignore
  4. Open the .dockerignore file in a text editor and add the following content:

    node_modules
    npm-debug.log

Deploying the application on Serverless Containers

  1. Build your image using the following command:
    docker build . -t <your application name>
  2. Push the created image into the Container Registry linked to your Containers namespace
    Tip

    The registry name starts with funcscw + the namespace name.

  3. Deploy the image using the Scaleway console.

Conclusion

You have built, pushed, and deployed your first web application using Node.js and Express Server on Scaleway Serverless Containers. For more information about the Containers product, refer to our dedicated product documentation.

API DocsScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCareers
© 2023-2024 – Scaleway