Header

Pre-requisites

  • Basic concepts of JavaScript
  • node installed on your system

Why NodeJS & ExpressJS?

NodeJS is a run-time platform or environment designed for server-side execution of JavaScript while ExpressJS is a framework built on NodeJS to simplify writing APIs with several features. As JavaScript is leading software industry in this era and it is super easy for beginners to dive into the development, Writing APIs in JavaScript(Node + Express) would be very beneficial.

Let’s Start Coding

Follow these steps

1. Create project directory

Create a directory and dive into it.

mkdir node-starter
cd node-starter

Open your favourite code editor and create a file named package.json and add {} in it.

2. Install basic librairies

Install ExpressJS & morgan. morgan is useful to get API logs in terminal.

npm install express morgan

3. Create server

After installation, create a file named server.js and add following code:

const express = require("express");
const morgan = require("morgan");

const app = express();

app.use(morgan("dev"));

app.listen(3000, () => console.log("Server started."));

4. Create Controllers

Controllers are method which are excuted against a specific route. Create a directory controllers in project root and create a file index.js in it. Add following code:

const sayHello = (req, res) => {
  return res.status(200).json({ message: "Hello!" });
};
module.exports = sayHello;

A controller takes two arguments as in our code req & res for request & response objects respectively. We can send a status & data in response as in our code 200 as status and an object having message Hello! in JSON format

5. Defining Routes

Here, routes are basically the endpoints where one can hit an API request.

Create a directory routes in project root and create a file index.js in it. Add following code:

const router = require("express").Router();
const sayHello = require("../controllers");

router.get("/hello", sayHello);

module.exports = router;

We define our router from express library’s function Router. Here we can tell the request methods to router like:

  • get
  • post
  • put
  • patch
  • delete

router.get (or any other method) is accepting two arguments here. First one is the endpoint and second is the controller which will be executed against this endpoint.

6. Binding router to server

Now it’s time to connect api router to our server.

Update server.js as:

const express = require("express");
const morgan = require("morgan");
const apiRouter = require("./routes");

const app = express();

app.use(morgan("dev"));
app.use("/api", apiRouter); //apiRouter connected to server

app.listen(3000, () => console.log("Server started."));

7. Hit an API endpoint

Run on terminal:

node server.js

Server will be started on the port you gave in app.listen method.

As we have created a get route, we can test it on our browser. Open your favourite browser and enter http://localhost:3000/api/hello. Moreover, you can test APIs on postman.

You will be responded with the message object by the server.

Client-Server

Create more routes, controllers and then connect with server as you want.

You can get this project on Github.