Node.js Ultimate Guide for Beginner — Part 3

Series - Node.js Ultimate Guide for Beginner

Before continuing, make sure to check the previous part here Node.js Ultimate Guide for Beginner — Part 2

You may want to read this article in Bahasa Indonesia version: Node.js Ultimate Guide untuk Pemula — Part 3

In this article, we’ll walk through how to create a Node.js project using npm init, push it to GitHub, and deploy it to Render. This tutorial is broken down into three parts: setting up the environment, deploying the app, and configuring it to be publicly accessible via a valid URL.

Preparing Your Node.js App for Deployment

In the first part, we’ll focus on getting the Node.js app environment ready for deployment. Here are the steps:

  1. Installing Node.js and Express

    We’ll use the Express framework to build a simple Node.js app. Express is one of the most popular frameworks for backend development.

    bash

    npm init -y
    npm install express
  2. Creating an API Endpoint

    Here’s a basic example of an endpoint that responds to a GET request and returns a list of users:

    javascript

    const express = require('express');
    const app = express();
    const PORT = process.env.PORT || 3000;
    
    const users = [];
    
    app.get('/users', (req, res) => {
        res.json({ users });
    });
    
    app.listen(PORT, () => {
        console.log(`Server running on port ${PORT}`);
    });
  3. Testing Locally

    Once the server is running, you can access the app at localhost:3000/users.

  4. Setting up package.json

    Don’t forget to configure the start script in package.json so you can run the app in the terminal using npm start.

    package.json

    {
      "scripts": {
        "start": "node index.js"
      }
    }

Starting Deployment on Render

Render is a platform that makes it easy to deploy apps. In this part, we’ll walk through configuring the app for Render.

  1. Creating a Render Account

    If you don’t have an account yet, you can sign up at render.com. Once you’re set up, make sure to link your GitHub account for easier deployment.

    Make sure to set up a payment method in your Render account to keep the app running long-term. This is required to keep your app continuously accessible.

  2. Adding Your App Repository

    Once your account is connected, you can add your Node.js app repository from GitHub.

    Managing your project on a platform like GitHub is essential. Here are a few key things to remember when uploading your project to GitHub:

    • README.md: This file is the “face” of your project. Be sure to explain what your project is about, list the required dependencies (like the Node.js version), how to install it, and how to run it locally.
    • LICENSE: Adding a license to your project is crucial. It protects your rights as a developer and also informs others about how they can use or modify your project.
    • CI/CD: Continuous Integration/Continuous Deployment (CI/CD) helps you automatically run tests and even deploy your project. GitHub provides GitHub Actions, which you can use to set up CI/CD workflows that run tests every time a new commit is made.
  3. Deployment Configuration

    In the Render dashboard, select Node.js as your app’s environment.

  4. Waiting for the Build Process

    Once everything is configured, Render will automatically build and distribute your app.

In this final section, we’ll ensure your app can be accessed using HTTPS by tweaking the port configuration.

  1. Port 443 for HTTPS

    When your app is deployed with HTTPS, it usually runs on port 443. This is the standard for secure connections, unlike HTTP, which uses port 80. We’ll need to change the previous port (3000) to 443.

    diff

    const express = require('express');
    const app = express();
    -const PORT = process.env.PORT || 3000;
    +const PORT = process.env.PORT || 443;
    
    // ...
    
  2. Testing the App

    Once the deployment is successful, you can test your app by visiting the domain provided by Render, such as https://your-app.onrender.com.

Penutup

Deploying a Node.js app to cloud platforms like Render or Heroku is pretty straightforward if you follow the steps outlined above. The key is making sure your app is properly configured and connected to your GitHub repository. Also, pay attention to the ports you use, especially when setting up HTTPS.

Once you’ve mastered these basics, you can explore deploying to other platforms or learning more about CI/CD for automated deployments. Make sure to test regularly and secure your app, especially if you’re using HTTPS and offering publicly accessible APIs.

Related Content