Gitlab - 4.1 Creating a NodeJs application
In this lesson we explore GitLab artifacts through a practical example that also touches on the wider pipeline workflow. We start from a new GitLab project named artefact-demo, clone it locally over HTTPS, switch to a working branch, and open it in Visual Studio Code so we can build a very basic Node.js application before pushing anything to the runner.
Node.js is a cross-platform, open-source JavaScript runtime that executes JavaScript outside the browser. It is widely used for server-side and networked applications that require a persistent connection, such as chat apps, news feeds and notification services. Alongside Node.js comes NPM (Node Package Manager), the default package manager that installs libraries, plugins and frameworks from the command line, for example with npm install express.
The application code
We create an index.js file and paste a minimal Express server. The first line imports the Express module, the next assigns the function call to an app object, a port variable is set to 80, a single GET endpoint returns a response message, and finally app.listen() starts the server with a startup log. Because the app depends on Express, we declare its dependencies in a package.json file placed at the project root.
{
"dependencies": {
"express": "^4.0.0"
}
}
To validate everything locally before writing the pipeline, we try to run the file with node index.js. The first attempt fails because the Express module is missing. We then try npm install, which itself fails because npm is not installed, so we install Node.js and NPM, re-run npm install to populate node_modules, and finally launch the app again. The server listens on localhost:80, opening the link returns the expected success message, and the project is now ready to be pushed back to the GitLab repository so we can describe these exact steps in a .gitlab-ci.yml pipeline in the next lesson.
Summary
This lesson covers creating a basic Node.js application using the Express web framework for GitLab CI/CD pipelines. The tutorial demonstrates how to write a simple Express server, understand the Node Package Manager (npm), and create a package.json file to manage project dependencies. The instructor walks through the application setup locally before explaining how to integrate it into a GitLab CI/CD pipeline for automated deployment.
Key points
- Node.js is an open-source JavaScript runtime that executes code outside a web browser, enabling server-side and real-time applications
- npm (Node Package Manager) is the default package manager for Node.js, allowing installation of libraries, plugins, and frameworks via command line
- Express is a standard web application server framework for Node.js that simplifies handling HTTP requests and responses
- A package.json file must be created to list all project dependencies (such as Express) and their versions
- The index.js file contains the application code, defining routes, ports, and server listeners
- Local testing of a Node.js application helps identify required installation commands and dependencies before writing the .gitlab-ci.yml pipeline configuration
FAQ
What is Node.js and why is it used?
Node.js is an open-source, cross-platform JavaScript runtime that runs code outside a web browser. It is used to develop server-side and network applications requiring persistent connections between browser and server, and is particularly suited for real-time applications like chat, news feeds, and notifications.
What is the purpose of the package.json file in a Node.js project?
The package.json file contains project metadata and is used to manage dependencies, scripts, version information, and other project settings. It ensures that all required npm packages (like Express) are properly listed and can be installed using npm install.
Why is local testing of a Node.js application important before creating a GitLab pipeline?
Local testing helps identify any missing dependencies, required installation commands, or errors that occur during execution. These insights inform the commands and configurations needed in the .gitlab-ci.yml file for automated building and deployment on the server.