Starting a simple RESTFUL API node.js project with express.js

In this article, I’ll show you the step by step process of starting a RESTFUL API using node.js and express.js .

Introduction:

Node.js is an open-source, cross-platform,back-end, javascript runtime environment that executes JavaScript code outside web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting— running scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser.

Express.js, or simply Express, is a back end web application framework for Node.js

Before Starting With Our Project:

If you have never used node.js or npm package you should install it first.

To check if node is already installed on your computer,

  1. Open your terminal,
  2. Type node -v or node — version
Node version

If your don’t return the version, then you must download node js from https://nodejs.org/en/ and download the latest lts(Long Term Support) version and we will also need a IDE to code. The preferred IDE for me is VSCode.

Initializing the node project

We need to start somewhere. I like to start by creating a folder and initializing a git repository in it.

Creating new folder and accessing the folder

After making new folder, we will initialize a node project,

Type command in terminal,

npm init

The generator will ask us few questions, it’s ok to just press [enter] for all of them. And at the end it will ask Is this OK? Type Yes.

This will create a package.js file in our project folder. Currently the package.js file will only contain information that we have entered. The information of all the dependencies can be seen here once installed.

Now we will open our project folder in an editor(using VSCode),

Then we will install express and setup the server. We need to run the server to make our API accessible to web browser or tools for testing the API like Postman. If you are not familiar with ExpressJS please refer to the official page.

Type command on the terminal with project folder dir:

npm i express
install express

Now create a new file server.js

Which will be the entry point for our project. Now put the following code in server.js folder

const express = require('express');const app = express();app.get('/', function (req, res) {res.status(200).send('Hello World!');});const port = process.env.PORT || 3000;const server = app.listen(port, function() {console.log('Express server listening on port ' + port);});

Here we are starting our project in port 3000. So, lets start by opening the terminal in our project folder and run the following command to start the node project.

node server.js

If our project run successfull it will display the message that we have logged.

After running the project, open postman or any other api testing app.

And make a get request to localhost:3000/

We will see Hello World! as a result.

This is a simple example of how we start a restful api project with node.js and express. We will be continuing this in the next article, on how to handle error cases for api not found, nodemon and handling cors.

Thank You