DEV Community

Cover image for Build A Hello World API With Node.js And Express.Js + Postman
M Ahsan Hameed
M Ahsan Hameed

Posted on

Build A Hello World API With Node.js And Express.Js + Postman

Getting Started

To get started create a new folder on any directory and call it hello_world.

$ mkdir hello_world

To move to that folder use this command.

$ cd hello_world

In your hello_world folder create a new file called app.js

$ touch app.js

The next step for us to do is to install Node. You can install node here.

We are close to create the app.

After node has been installed then we’ll install express with npm. Node comes with npm so we won’t need to install npm.

npm init

Click the return key repeatedly for every questions asked of you on the command line.

Install Dependencies

npm install express —-save

Setup The App And Create Our First Endpoint

Now let’s get started creating our hello_world API in the app.js file.

const express = require('express');
const app = express();

Setting Server Port

Add more code in our app.js file.

const port =8000;
app.listen(port,()=> {
console.log(
Server is listening on port ${port});
})

Create API

Let's start writing code for our first API.

app.get('/hello_world', (req, res)=>{
res.send('Hello World');
})

get shows the HTTP GET
First parameter is our API path
Second parameter a callback function
Callback function first request shorthand req
Callback function first response shorthand res

Start Server

Hurrah...! We are just created the server with hello world API next step is pretty easy to start the server run that command.

$ npm start

Check postman

Image description

Pretty Easy Congrats you tested it....?

Follow For more update and stay connected

Image description

Top comments (0)