To initiate the project, please submit a project creation request to the administrator via the provided form. Upon approval, the administrator will grant access to a Git repository hosted on GitHub. Authorized users will then have the capability to create branches within this repository, facilitating collaborative development and version control.

Remember: the 'main' branch cannot be modified directly, each developer need to use branches and create a github Pull Request(PR) to merge desidere branches into main

Project Overview

The cloned repository will have a structure like this:


This document provides a detailed description of the organizational structure of the project, including directories and files, along with their respective purposes:

Each component of the project's structure is designed to support its development, deployment, and documentation, ensuring a streamlined workflow for contributors.

Initial file content

here we are going to give an overview of the file that has been created for the startup project:

src/index.js

const express = require("express");
const app = express();
const port = 3000;

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


const server = app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

module.exports = { app, server }; // Export both app and server

Overview

This file is a demo that uses the express framework to create route that can be accessible via HTTP

Key Components

Expanding the Application

To build upon this demo, consider adding more routes, middleware for handling various HTTP methods, static files, or even integrating with a database. Express makes it easy to scale applications from simple demos to full-featured web applications.

Conclusion

This demo application provides a basic introduction to creating web servers with Node.js and Express. By understanding how to set up a simple server and respond to HTTP requests, you can begin to explore more complex applications and APIs using Express.

Exporting

The application exports both the app and server objects, allowing for easy integration and testing within larger applications or microservices. This practice is beneficial for unit testing or when importing the application as a module in other parts of a project.

Additional services needed by the application during the test

Probably the NodeJS backend in development would need to talk with other service like database, s3 storage or others. For this reason use docker to help bringing up a development environment. Create a docker-compose.yaml file with all the needed service, ad example to integrate a PostgreSQL database could be used this docker-compose.yml file content:

version: '3.8'
services:
  db:
    image: postgres:latest
    environment:
      POSTGRES_DB: demodb
      POSTGRES_USER: demo_user
      POSTGRES_PASSWORD: demo_pass
    volumes:
      - ./init-db:/docker-entrypoint-initdb.d # Mount the directory containing demo_schema.sql
    ports:
      - "5432:5432"
    restart: unless-stopped

demo_schema.sql is a file contained into the init-db folder that can contains somenitng like:

-- Create a simple table named 'demo_table'
CREATE TABLE demo_table (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

to startup the docker compose service run the following command:

docker-compose up -d

at this point the PostgreSQL service is accessible from the localhost:5432 tcp address. To create more comples docker-compose file read the official documentation.

test/index.test.js

const { server } = require("../src/index"); // Adjust the path as necessary
const request = require("supertest");

describe("index.js", function () {
  after(function () {
    server.close(); // Close the server after tests
  });

  it("Test echo root path", function (done) {
    request(server).get("/").expect(200, done);
  });
});

Overview

The test uses supertest, a popular testing library for HTTP assertions, allowing us to test the response of our Express application without starting the server manually. This file contains a single test suite that verifies the server's response to a GET request at the root URL.

Dependencies

Test Suite: index.js

After Hook

Test Cases



Initiating Application Development

With the preparatory steps completed, you are now ready to embark on the development of your application. To ensure a smooth and efficient workflow, it is recommended to adhere to the following guidelines:

  1. Branch Creation:

  2. Commit and Push:

  3. Testing:

  4. Pull Request and Merge:

  5. Deployment Workflow:

Following these steps not only ensures a systematic approach to application development but also fosters a culture of testing and continuous integration, thereby enhancing the quality and reliability of your application. Upon completion, the cycle can recommence with the creation of a new branch for additional features, returning to the beginning of this guide.

Related issues