Set up a NodeJS API With CI on Gitlab for Your Web/Mobile App (Express/Jasmine/PostgreSQL/Docker) part-1/3

Charles Touret
Dec 28, 2020 · 7 min read

Introduction

In this document I will explain how to build a rest API with automatic tests on managed by Gitlab-CI. At the end of this document you will be able to build your own API and especially to maintain it ! You will also be familiar with docker and the node testing framework Jasmine. The API that you will create can be used for your web application or/and for your mobile application.

The more you test the more you prevent bugs

When you are working on your API, you create different routes/endpoints (https://myAPI/login for example), but how can you be sure that all possible behaviors on your routes will work as you expected? Imagine that an user try to login with bad credentials format ? Your API should return an error and not return a valid response, this way it will let the user enter in your application !

So to make sure that all the behaviors works on a route, you should (ideally) test all behaviors possible on it! You also have to test the normal operation to make sure it’s working, so when a user login with the rights credentials, you should verify that the HTTP response code is 200 and that the response body really contains what you expected for a good behavior.

When you add code on your API, It’s very important to check that you didn’t break anything. To notice that, you should launch your tests each time you commit on Gitlab, and if one test does not pass, it means that you broke something, and you should fix what you broke before push !

To launch API tests, you can do it manually on your computer, before push your code, you can launch tests in your terminal and check if they are all good. But if you want to do it regularly to prevent bugs (for example 1 time each days), or at each push, you should use continuous integration, I will explain how to do it with gitlab-ci.

Create your NodeJS API

Before test, we need something to test ;) so let’s create our Node API connected to postgreSQL ! To do it you will need to have node.js 12.19.0 +, postgresql 9.6.19+ and you’ll need to know how to use postman (you can find how to use it on this article section “make API calls in local with postman”).

  • To download nodeJS on linux :
sudo apt install nodejs

once installed, check the version to verify installation works

node -v or node --version

You will also need to install npm, npm is the package manager of node, this is with npm that you can install libraries for your node project.

sudo apt install npm
npm -v or npm --version
  • To download postgreSQL :
sudo apt-get update
sudo apt-get install postgresql
sudo service postgresql

You must have this output :

'Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status} [version ..]'
  • To download and use postman check this article and look the first section

After the set up, you can now init your project

npm init

it will create a package.json file at the root of your project, this is in this file that all your libraries (like express) will be listed.

Then install the package express, you will need it to launch an express server, so run in a terminal at the root of the project :

npm install --save express

After these steps, create a file named index.js at the root of your project, and copy/paste the follow code inside :

// import express (after npm install express)const express = require(‘express’);// create new express app and save it as “app”const app = express();// server configurationconst PORT = 8080;// create a route for the appapp.get(‘/’, (req, res) => {res.send(‘Hello World’);});// make the server listen to requestsapp.listen(PORT, () => {console.log(`Server running at: http://localhost:${PORT}/`);});

then launch at the root of your project

node index.js

it will show :

Server running at: http://localhost:8080/

right click on the http link and open it on a navigator (or copy paste the link directly in a navigator)

You will normally see this in your navigator :

it means you successfully launch the express server ! well done !

connect to postgres with user postgres

psql -U postgresyou will see this in the terminal
postgres=#

create your database

postgres=# CREATE DATABASE medium;

now list your databases to verify database ‘medium’ is created

postgres=# \lList of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+----------+----------+-------------+-------------+-----------------------
medium | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8

now create a table users and add a data in it

  • connect to your database
postgres=# \c medium
  • create users sequence to auto-increment id primary key of the future table users
medium=#
CREATE SEQUENCE public.users_id_seq
INCREMENT 1
START 0
MINVALUE 0
MAXVALUE 9223372036854775807
CACHE 1;
  • create users table
medium=#
CREATE TABLE public.users
(
id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
first_name text COLLATE pg_catalog."default" NOT NULL,
last_name text COLLATE pg_catalog."default" NOT NULL,
email text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT user_pkey PRIMARY KEY (id),
CONSTRAINT unique_email_constraint UNIQUE (email)
)
  • add a data in users table
medium=#
INSERT INTO users (first_name, last_name, email) VALUES ('user', 'un', 'user.un@test.com');
  • verify that data is in the table users
medium=#
SELECT * FROM users;
id | first_name | last_name | email
----+------------+-----------+------------------
0 | user | un | user.un@test.com
  • install postgres dependency for your node project
npm install --save pg

create a config file for postgres at the root of your project called db_config.js and write this inside :

// config
// PostgreSQL config
const configPG = {
user: 'postgres',
host: 'localhost',
database: 'medium',
password: 'postgres',
port: '5432'
}
const Pool = require('pg').Pool
const db = new Pool(configPG)
module.exports = { db }
  • create a file called queries.js at the root and copy/paste this inside :
const { db } = require('./db_config')const getUsers = (request, response) => {
const query = 'SELECT * FROM users'
db.query(query, (error, results) => {
if (error) {
response.status(500).json(error)
}
response.status(200).json(results.rows)
})
}
module.exports = {
getUsers,
}

this is the file organization that you should have at this point

  • modify your index.js file like this :
// import express (after npm install express)
const express = require('express');
const queries = require('./queries')
// create new express app and save it as "app"
const app = express();
// server configuration
const PORT = 8080;
// create a route for the app
app.get('/', (req, res) => {
res.send('Hello World');
});
// create a route to list database users
app.get('/users/', queries.getUsers)
// make the server listen to requestsapp.listen(PORT, () => {
console.log(`Server running at: http://localhost:${PORT}/`);
});

Now launch your server ( node index.js) and go to http://localhost:8080/users !

you should see this :

Congratulation you’ve made it ! You have now an express API connected to your postgreSQL, this is the first step to create a backend of an application ! we will see then how to test your API routes, but first let push the code on gitlab !

Push your NodeJS API on GitLab

  • go to https://gitlab.com
  • new project -> create a blank project (after login)
  • download git CLI on your computer

sudo apt-get update

sudo apt-get install git-all

  • go at the root of your project in your computer
git init
git remote add origin https://gitlab.com/yourUserName/project_name.git
git add .
git commit -m "Initial commit"
git push -u origin master

don’t forget to replace ‘yourusername’ and ‘project_name’

your project is now save on gitlab, and you can see it by refreshing your project’s gitlab page, well done !

How/Why to use an API on my applications ?

You can now use your API in your applications, but how to do it ?

We saw in this document how to make an API call in the url of a browser, if you want to do it from a web framework (like reactJS) you can do it by using a library like request or axios that allows you to make HTTP requests !

With axios and request you will be able to make all HTTP requests (post, get, etc…) from your front, and then get back the request result in a variable that you will display.

Same way for a mobile application, there are libraries that allows you to perform HTTP requests, if we want to do it in react-native for example, we can also use the library named requests.

Why we want to use an API to communicate with our database ? It’s because with an API we can add security on our data and we have a standard way to access our data, that means that from all programming languages, we will access our data with HTTP requests.

About the security on an API I am currently writting an article on this, and it will be available there, but if you are not familiar with tests and API I strongly recommend you to continue to the second part of this article that will be available next week ;)

Thanks for reading.

WantedOffice

WantedOffice is a new way of work, work spaces in private homes, for teleworkers and companies.

Charles Touret

Written by

WantedOffice

Businesses cut costs and teleworkers feel more productive with a change of scenery. This publication will talk about remote work, new working practices, employee well-being and there will also be some technological publications about how we are working within WanteOffice.

Charles Touret

Written by

WantedOffice

Businesses cut costs and teleworkers feel more productive with a change of scenery. This publication will talk about remote work, new working practices, employee well-being and there will also be some technological publications about how we are working within WanteOffice.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store