Skip to main content
In this tutorial, you’ll clone, configure, and run a Node.js REST API locally before integrating it into a GitHub Actions workflow. By the end, you’ll have a working Express/Mongoose application, a full test suite, and coverage reports.

Prerequisites

Ensure you have Node.js and npm installed. On Ubuntu/Debian:
Verify your installations:

1. Clone the Repository

Fetch the source code from GitLab and navigate into the project folder:

2. Install Dependencies

Install all production and development packages defined in package.json:

3. Review package.json

Open package.json to understand scripts, coverage settings, and dependencies.

Scripts Overview

NYC Coverage Configuration

Dependencies

Dev Dependencies


4. Application Entry Point (app.js)

This file initializes Express, connects to MongoDB via Mongoose, and defines your REST API endpoints.
Never commit real credentials. Use environment variables or a secrets manager.

5. Test Suite (app-test.js)

Uses Mocha, Chai, and Chai HTTP to validate your API endpoints.

6. Client Logic (client.js)

A simple front-end script that fetches all planets on page load:

7. Dockerfile

Containerize the application for consistent deployment:

8. Kubernetes Service Manifest

Expose the application via a NodePort service:

9. Running Tests Locally

9.1 Without Environment Variables

You’ll encounter:

9.2 Temporary Hard-Coding (Demo Only)

Replace the mongoose.connect call in app.js:
Then:
Expected output:
A test_results.xml file will be created for CI consumption.

10. Generating Coverage Reports

If coverage falls below the 90% threshold, you’ll see:
Coverage artifacts are saved under coverage/, including:
  • cobertura-coverage.xml
  • coverage-summary.json
  • lcov report

11. Start the Application

Run the server locally:
Visit http://localhost:3000 in your browser. Use the search bar to query planets by ID.
The image shows a webpage with a space-themed background, featuring a "Solar System" search interface and information about Earth.
Tip: Search “3” for Earth or “6” for Saturn.
The image shows a webpage about the solar system, featuring an illustration of Saturn with its rings and a description of the planet. There is a search bar labeled "Search the Planet" and a title "Solar System" at the top.

You’re now prepared to integrate this setup into your GitHub Actions workflow for CI/CD automation.

References

Watch Video