Getting Started: Building Location-Based (GIS) REST APIs with NodeJS

Faith Muchembi
5 min readFeb 7, 2021

--

Express and PostgreSQL

- What is NodeJS?

Nodejs is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.js can be executed within the JavaScript runtime (includes everything you need to execute a program written in JavaScript)on OS X, Microsoft Windows, and Linux.

Now that we know what NodeJS is and what it does we shall proceed to the project overview the prerequisites then get started.

- Project Definition

We will build a GIS REST API for mainly two datasets i.e. Nairobi Health Facilities and Nairobi Sub Counties, an add on shall be getting heath facilities within a subcounty and the nearest health facility basing from the user location. We shall also test the Api by fetching the data using postman

Here is a link to the datasets used in this project(Click here)

- Project Prerequisite

You need to have PostgreSQL , NodeJS installed and have at least a basic understanding of the JavaScript programming language. Incase you neither have PostgreSQL nor NodeJS look at their documentation for installation, its pretty straight forward.

Database setup — Lets open up pgAdmin, it is a tool that comes with PostgreSQL but it is always installed separately from PostgreSQL. It is with pgAdmin that we create databases or with a terminal command psql. PgAdmin should have postGIS as an enabled extension, if postGIS is not installed, you can install it by right-clicking the extensions branch, choose new extension then select postGIS from the menu. PostGIS (is a free and open source library spatial database extender for PostgreSQL. It adds support for geographic objects allowing location queries to be run in SQL), we shall name our database Nairobigis, to our database we shall add our GIS data, the Nairobi Health Facilities and Nairobi Sub Counties these are the database tables, having trouble setting up the database? see the Setting up PostGIS tutorial.

Basically your database should appear as the image below.

Nairobigis database

Creating our NodeJS application.

Open command prompt, connect to your preferred location on your laptop and create a new folder to hold our application, I will use my desktop, create a new folder by using mkdir command lets name it server, then connect to the created folder by typing in your terminal cd server, in the folder we shall npm init, npm init creates a package.json file that keeps track of all our installed dependencies.


> cd Desktop/gis-rest-api-with-nodejs
> mkdir server
> cd server
> npm init

We shall now install the dependencies we need i.e. express, pg, cors and router


> npm install express pg cors router

Express — this a framework that allows create a server in NodeJS

pg — connects database to server, enables running of some sql queries

cors — Enables two different domains to interact with each other

Router — It basically enables the mounting of middleware, which are being served by the specific route

After the installation of all the required dependencies we create a new file in the server folder, lets name it index.js

> cd Desktop/gis-rest-api-with-nodejs/server $touch index.js

In the index.js file we shall import our installed dependencies as requirements from the package.json file.

const express = require('express');
const cors = require('cors');
const app = express();
//middleware
app.use(express.json());
app.use(cors());
app.listen(9000, () => {
console.log("server started on port 9000");});

use npm index to start your server and incase you have nodemon installed use nodemon index. This is what you should see

Starting our local server

Connecting our application to our database in PostgreSQL —Now, lets create a new file and name it db.js

> cd Desktop/gis-rest-api-with-nodejs/server $touch db.js

In the db.js lets write this code. This code connects our application to our local database in PostgreSQL.


const Pool = require("pg").Pool;
const pool = new Pool({
user: "postgres",
password: "1234",
host: "localhost",
port: "5432",
database: "nairobigis"
});
module.exports = pool;

we now need to import our pool into the index.js, this is how our index.js will now look.

const express = require('express');
const cors = require('cors');
const app = express();
const pool = require("./db");
//middleware
app.use(express.json());
app.use(cors());
//routes
app.use("/data", require("./routes/nairobiHealth"));
app.listen(9000, () => {
console.log("server started on port 9000");})

Next we shall create a new folder within the server, lets name it routes because it holds the routes(links) to help us fetch the data. Within the routes folder lets create a file and name it nairobiHealth.js

> cd Desktop/gis-rest-api-with-nodejs/server 
> mkdir routes
> cd routes
> touch nairobiHealth.js

Getting all the health facilities in Nairobi and all Nairobi sub counties

In the nairobiHealth.js we shall now create function to fetch all the health facilities within Nairobi, all the sub counties in Nairobi.

Have a look at the code below.

We now need to update our index.js file by connecting it to our nairobiHealth.js file using routes. This is how our updated index.js file will look like.

It is time to test our REST API, we shall use postman, make sure your server is running before sending the requests.

Nairobi county health facilities

and yes we can fetch the health facilities data, let us try out fetching the sub counties data. Our data is fetched as shown in the image below

Nairobi sub-counties data

Getting health facilities within a subcounty.

Getting Nearest health centers

Nearest health centers

- Conclusion

Our REST API is working as expected, congratulations for pulling through and successfully building your first JavaScript Restful API, there’s still more to explore and learn, so be sure to click on all the links in the article to learn more.

--

--

Faith Muchembi
Faith Muchembi

No responses yet