NodeJS is most normally utilized with Express system. Additionally a group of other outer libraries are utilized with NodeJS. 

The Reason these systems and libraries are utilized alongside NodeJS is to make advancement considerably more less demanding and faster. 

While chipping away at any genuine task, it is best to utilize structures and libraries wherever expected to empower snappier improvement 

All things considered, in this post I will demonstrate to assemble Simple REST API's with NodeJS without utilizing express structure or some other outside libraries. This article will utilize just those functionalities that are given NodeJS itself. 

The explanation behind this is to demonstrate how NodeJS can be utilized without structures and libraries also ????. Likewise this will give a superior thought with respect to how NodeJS functions ???? 

Pre-essential 

Introduce NodeJS from https://nodejs.org 

Code 

The code for this article is accessible in my github repo. 

How about we begin with the code ???? 

Make an envelope called as basic rest-apis-nodejs-without-systems. This will be our NodeJS Project envelope. 

Go into the venture organizer and utilize npm init to make the undertaking into a hub venture. The directions to do this are 

cd basic rest-apis-nodejs-without-structures 

npm init 

package.json 

Subsequent to running npm init a package.json record is made inside the undertaking organizer. 

package.json has data about your task like undertaking name, form, portrayal and so on. Likewise package.json is the place you will include hub conditions. In this article we won't have any conditions since we are utilizing just functionalities that are given by NodeJS itself. 

First API 

Make a record called server.js inside the task organizer. This will be the beginning stage of our application. 

Duplicate the accompanying code into server.js 

const hostname = '127.0.0.1'; 

const port = 3000; 

const server = require('./controller.js'); 

server.listen(port, hostname, () => { 

console.log(`Server running at http://${hostname}:${port}/`); 

}); 

This code really depends on a document called as controller.js which we will include soon. This code is advising that a server needs to tune in on port 3000 and in localhost 

The server creation is done in controller.js 

controller.js 

This is where we will make the server and characterizing our rest endpoints. Make a document called as controller.js 

How about we make a solitary GET endpoint first in controller.js 

const http = require('http'); 

const url = require('url'); 

module.exports = http.createServer((req, res) => { 

var benefit = require('./service.js'); 

const reqUrl = url.parse(req.url, genuine); 

/GET Endpoint 

in the event that (reqUrl.pathname == '/test' && req.method === 'GET') { 

console.log('Request Type:' + 

req.method + ' Endpoint: ' + 

reqUrl.pathname); 

service.sampleRequest(req, res); 

}); 

First http and url modules are foreign made. These modules are given by NodeJS itself. 

http module empowers to make web applications. It underpins both customer and server activities. 

url module helps in parsing urls 

The line http.createServer((req, res) => { demonstrates that a http server should be made with demand as req and reaction as res 

module.exports is utilized to send out this document as a module. This is the reason we could import controller.js in server.js utilizing const server = require('./controller.js'); 

It tends to be seen that this document requires service.js which we will discuss later. 

The code const reqUrl = url.parse(req.url, genuine); gets the demand url and parses it with the goal that we can run some url capacities on it. 

The First endpoint we will make is a GET endpoint with endpoint url as/test 

With the end goal to do url steering we will utilize if else conditions 

The line if (reqUrl.pathname == '/test' && req.method === 'GET') { checks if the url being asked for is/test and furthermore checks if the demand type is GET 

The rationale for this get ask for is available in service.sampleRequest(req, res); which is a capacity characterized in service.js 

service.js 

This is the place the genuine programming interface rationale will be available. Make a record called as service.js. 

Duplicate the accompanying code into service.js 

const url = require('url'); 

exports.sampleRequest = work (req, res) { 

const reqUrl = url.parse(req.url, genuine); 

var name = 'World'; 

in the event that (reqUrl.query.name) { 

name = reqUrl.query.name 

var reaction = { 

"content": "Hi " + name 

}; 

res.statusCode = 200; 

res.setHeader('Content-Type', 'application/json'); 

res.end(JSON.stringify(response)); 

}; 

This code checks if the demand URL has an inquiry parameter called as name and stores it in name factor. On the off chance that no question parameter is available, it defaults to the string World 

The reaction status is set as 200, Content Type of the reaction is JSON lastly the reaction is sent back utilizing res.end(JSON.stringify(response)); 

since reaction variable is a JSON Object, we are utilizing JSON.stringify on it to change over it to string before sending back the http reaction 

What is NodeJS?

Presently we can run the application utilizing the order 

hub server.js 

Testing 

With the end goal to test the endpoint utilize postman. You can download it from here 

In postman select Get ask for and type the url as http://localhost:3000/sample?name=aditya and hit send 

The yield for this demand is demonstrated as follows 

"content": "Hi aditya" 

Presently type the url as http://localhost:3000/test and hit send 

The yield for this demand is demonstrated as follows 

"content": "Hi World" 

Second API 

In this area we will fabricate the second API which is a POST ask. Likewise if client hits some arbitrary url, we should demonstrate that it is an invalid course. We will include the rationale for that also here. 

controller.js 

Refresh the code in controller.js to the one demonstrated as follows 

const http = require('http'); 

const url = require('url'); 

module.exports = http.createServer((req, res) => { 

var benefit = require('./service.js'); 

const reqUrl = url.parse(req.url, genuine); 

/GET Endpoint 

on the off chance that (reqUrl.pathname == '/test' && req.method === 'GET') { 

console.log('Request Type:' + 

req.method + ' Endpoint: ' + 

reqUrl.pathname); 

service.sampleRequest(req, res); 

/POST Endpoint 

} else if (reqUrl.pathname == '/test' && req.method === 'POST') { 

console.log('Request Type:' + 

req.method + ' Endpoint: ' + 

reqUrl.pathname); 

service.testRequest(req, res); 

} else { 

console.log('Request Type:' + 

req.method + ' Invalid Endpoint: ' + 

reqUrl.pathname); 

service.invalidRequest(req, res); 

}); 

The post endpoint will have the url/test. This code has a condition to check for/test endpoint. The rationale for/test endpoint will be in service.testRequest(req, res); which is in service.js 

This code additionally has an else condition for invalid courses. The rationale for invalid courses is taken care of in service.invalidRequest(req, res); 

service.js 

Include the accompanying bit of code to service.js. Try not to erase the current code in service.js. Include this code underneath the current code. 

exports.testRequest = work (req, res) { 

body = ''; 

req.on('data', work (lump) { 

body += lump; 

}); 

req.on('end', work () { 

postBody = JSON.parse(body); 

var reaction = { 

"content": "Post Request Value is " + postBody.value 

}; 

res.statusCode = 200; 

res.setHeader('Content-Type', 'application/json'); 

res.end(JSON.stringify(response)); 

}); 

}; 

For a POST ask for, an info POST body is given when the endpoint is called. 

In the code we should get this POST body from the demand. 

The accompanying code does this 

req.on('data', work (lump) { 

body += lump; 

}); 

The ask for comes as a stream. This code takes the flood of information and continues affixing it to body. 

req.on('end', work () { is executed simply after the gushing is finished and the full post body is gotten. 

postBody = JSON.parse(body); This code changes over the info post body into JSON organize with the goal that we can utilize the qualities in it. 

in the code we are utilizing esteem field in the postBody. 

The reaction is set like how we did it for GET ask. 

rationale for invalid demand 

Include the accompanying bit of code to service.js. Try not to erase the current code in service.js. Include this code beneath the current code. 

exports.invalidRequest = work (req, res) { 

res.statusCode = 404; 

res.setHeader('Content-Type', 'content/plain'); 

res.end('Invalid Request'); 

}; 

For an invalid demand, the status is set to 404 and content sort is set to content. The real substance sent back is Invalid Request 

Testing 

Go to postman. Select the demand Type as POST and type the accompanying url http://localhost:3000/test. 

Additionally select Body , crude and application/json as appeared in the picture 

The information Post body is given beneath 

"esteem" : "nodejs" 

Tap on send in postman 

The API yield is given underneath 

"content": "Post Request Value is nodejs" 

You can likewise attempt with an Invalid ask. In postman select GET and type the url as http://localhost:3000/test123 

The reaction for this will be Invalid Text 

Well done ???? 

You presently realize how to make REST API's in NodeJS without utilizing any system or outside library ???? 

In Real Projects dependably utilize a system or library wherever conceivable to make dev cycle less demanding and speedier 

About the creator 

I cherish innovation and pursue the headways in the field. I additionally like helping other people with my innovation information. 

Don't hesitate to associate with me on my LinkedIn account https://www.linkedin.com/in/aditya1811/ 

You can likewise tail me on twitter https://twitter.com/adityasridhar18 

My Website: https://adityasridhar.com/ 

Different Posts by Me 

A prologue to Git 

Choosing Best NodeJS Frameworks