Building RESTful API using Nodejs and Express
Published: 2021-06-11 09:21:49

Working on the front-end using HTML, CSS & Javascript and wondering how RESTful API works in the backend. Then you can start working on it using Nodejs.

## What is Nodejs?
Node.js is a server-side programming framework built on Chrome's JavaScript engine, which provides event-driven and non-blocking I/O. The coding is done in Javascript, and its latest version is v11.9.0. Read the [docs](https://nodejs.org/en/docs/) for further information.

## Nodejs installation guide:
1. Download the Nodejs application from the official Node.js official site: https://nodejs.org/en/download/ Follow the steps depending on your OS.
2. After installation, check for the version of Node and NPM using the command in your terminal NPM is the Node Package Manager and is a tool for installing, uninstalling and maintaining package modules for your app. NPM is installed along with Node:
```bash
node -v
npm -v
```
![Node - npm version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pjb4jqslsd1ojfenhvl.png)

After verifying the versions of both Node and NPM, now create a new folder for our server.
Run the below commands in your terminal to create a new folder, and inside a folder, create one new file, index.js.

```bash
$ mkdir SampleServer
$ cd SampleServer
SampleServer$ touch index.js
```
After successfully creating folders and files, run the following code:
```bash
SampleServer$ npm init -y
```

Running the command npm will create a new file package.json.

**package.json** - It holds various metadata/dependencies relevant to the project. This file gives information to npm, which allows it to identify the project and handle the project's dependencies.

![package.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbwjags3nfc07vavbv7w.png)

After configuring Node and npm, now it's time to install express. ExpressJS is a web framework for Node that provides you with a simple API to build websites, web apps and backends. With ExpressJS, you don't need to worry about low-level protocols, processes, etc. In order to install express, run the following command to add the framework.

```bash
SampleServer$ npm install --save express
```

This will add all the dependency of express in the server application.

![dependecy-structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6m70zwrllzvup1kdmw5z.png)

Now it's finally time to write some codes. Add the following code to your index.js file.

```js
var express = require('express')
const app = express()
app.get('/', res, req => {
res.send("Hello world!")
})
app.listen(3000, function() {
console.log('App running in port 3000');
});
```
A variable name 'express' is holding a new express application.
app.listen(3000) implies that the server is listening local in port number 3000. Now run the server by using the following command.

```bash
SampleServer$ node index.js
```

![terminal output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jg5ozt1ja4qer9wpzxiv.png)

Now request the server which you've just created from the browser.

![web output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh8y1lkr4yfht9n9k9qu.png)

You successfully created a sample server.

Few important express methods to be familiar with:
- **app.listen()**: To listen to the server in a particular port.
- **app.get()**: The GET method requests data from the server. Requests using the GET method should only retrieve data and should have no other effect.
- **app.post()**: The POST method requests the server to accept data that is enclosed in the request as a new object/entity of the resource identified by the URI.
- **app.put()**: The PUT method requests the server to accept the data enclosed in the request as a modification to existing data identified by the URI. In case data does not exist, then the PUT method should create one.
- **app.delete()**: It is a delete method for express. The DELETE method requests the server to remove/delete the specified resource.

Did you enjoy this? Please give it a like so others can discover it as well.

You may also like my other articles:

1. <a href="https://pratapsharma.in/regex-with-javascript/" target="_blank">Console.log and other console methods to debug your code in JavaScript</a>
2. <a href="https://pratapsharma.in/things-to-keep-in-mind-before-learning-javascript-framework/" target="_blank">Things to keep in mind before starting Javascript framework</a>
3. <a href="https://pratapsharma.in/difference-between-var-let-const-in-javascript/" target="_blank">var, let, and const – Why to avoid var 😷 and how to put the other two to good use? - Javascript</a>

If you liked the article, feel free to share it to help others find it!

You may also follow me on <a href="https://www.linkedin.com/in/prataps22/"  target="_blank">LinkedIn</a> and <a href="https://twitter.com/Pratap2210"  target="_blank">Twitter</a>.

💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter [here](https://pratap.substack.com).