Build Up a Simple Backend with Express.js, Scrapy, PostgreSQL, and Heroku — Express.js

Laurence Liu
4 min readOct 24, 2019

There are several languages and popular frameworks that could help backend developers to create RESTful API, such as Java/Kotlin with Spring, C# with ASP.NET, Python with Flask/Django, Go with Golang, PHP with Laravel, Node.js with Express.js, etc. The most important thing is all of those popular frameworks could solve the requirements of most web applications.

The reason why I choose Node.js is just that is very easy to learn. When it comes to performance issues, this article may help.

Node.js and Express.js

Here is the official website of Node.js, just click the download to install the Node.js and npm.

Express.js is the framework of Node.js. It provides a lot of flexible APIs to make things simple.

There is some brief content about Node.js. And the advantages of it.

So, the only thing you need to know before creating a project is the syntax and characteristic of Javascript.

Starting with Hello World

The official site of Express.js already provided an example for Hello World. Just follow those steps and you will get your first Express.js project.

Note: Don’t forget to generate an express project before you start. Or there is an easy way is to use the commands below.

$ npm init
$ npm install express

Install libraries to boot up the speed

  • Knex.js — A batteries-included, multi-dialect (MSSQL, MySQL, PostgreSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for Node.js.
  • cors — CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.
  • body-parser — Node.js body parsing middleware.
  • node-postgres — Non-blocking PostgreSQL client for Node.js. Pure JavaScript and optional native libpq bindings.
$ npm install body-parser
$ npm install cors
$ npm install knex --save
$ npm install pg

After running the npm command for each library, the dependencies part of package.json may look like below.

Connect Express.js to PostgreSQL

After created a Hello World, the next task is to connect to the database. And you can fetch and arrange those data into RESTful APIs.

Create a Controller to fetch data

There is a website that explains the RESTful API and the methods. Basically, “GET” and “POST” are enough for a tiny project. But it’s good to read more about them.

Here, it’s time to add a controller to fetch data from the database. The picture below shows the explain about Express.js API.

And these are the code which will be add in this section.

The page param will be used for the pagination function later. And I set the limit to 10, so this will return data of id 1 to 10 from the database. This part is just like the SQL command below.

SELECT * FROM cocktails LIMIT 10;

The res.json will help you to deal with the JSON format. It is pretty simple and fast for someone who wants to build up a RESTful API.

Run the Project

Now, just run this project with the command “npm start” and you will get your first RESTful API which fetches data from the database.

Note: You will get a page which is full of JSON format via access the URL like, http://localhost/cocktails or something.

Bonus: Add pagination for Cocktails API

A lot of requirements are able to implement by search the Query Builder in the document of Knex.js.

Take pagination as an example. Because pagination is relative with range. So the first thing comes to my mind is the syntax “whereBetween”.

After some modify as below, and you will get a whole new API with customizing JSON object and request param.

Note: “whereBetween” may not be the best way to pagination issue. I made some change with the syntax “orderBy” and “limit”in this project after I wrote those.

So far, everything is set up, but all of them are running locally. In the next section, I will put everything on the Heroku cloud.

--

--