Building NodeJs API without ExpressJs or any other framework

$ mkdir just-node-api
$ cd just-node-api
$ npm init -y
$ touch server.js
const http = require('http');const hostname = '127.0.0.1';const port = 3000;const server = require('./route.js'); // imports the routing fileserver.listen(port, hostname, () => {  console.log(`Server running at http://${hostname}:${port}/`);});
module.exports = users = [ {
id: 1,
name: 'Seyi',
location: 'Buenos Aires'
},
{
id: 2,
name: 'David Khan',
location: 'Argentina'
},
{
id: 3,
name: 'Isaac',
location: 'Kwala lumpor'
}
];
const http = require('http');
const url = require('url');
module.exports = http.createServer((req, res) => {
var userOps = require('./controller.js');
const reqUrl = url.parse(req.url, true);
// GET endpoint
if(reqUrl.pathname == '/users' && req.method === 'GET') {
console.log('Request type: ' + req.method + ' Endpoint: ' + req.url);
userOps.getUsers(req, res);
}
}
})
const url = require('url');
const users = require('./userData.js');
exports.getUsers = function(req, res) {
const reqUrl = url.parse(req.url, true)
var response = [
{
"message": "Here are the list of users "
},
users
];
res.statusCode = 200;
res.setHeader('content-Type', 'Application/json');
res.end(JSON.stringify(response))
}
Server running at http://127.0.0.1:3000/
127.0.0.1:3000/users
...
// POST endpoint
else if(reqUrl.pathname == '/user' && req.method === 'POST') {
console.log('Request type: ' + req.method + ' Endpoint: ' + req.url);
userOps.createUser(req, res);
}
...
...exports.createUser = function(req, res) {
body = '';

req.on('data', function (chunk) {
body += chunk;
});

req.on('end', function () {
postBody = JSON.parse(body);
var response = [
{
"text": "User added successfully"
},
postBody
]

res.statusCode = 201;
res.setHeader('content-Type', 'Application/json');
res.end(JSON.stringify(response))
})
}
...
req.on('data',  function (chunk) {
body += chunk;
});
the response got from running POST 127.0.0.1:3000/user
...// invalid URL
else {
console.log('Request type: ' + req.method + ' Endpoint: ' + req.url);

userOps.invalidUrl(req, res);
}
...
...const availableEndpoints = [
{
method: "GET",
getUsers: "/users"
},
{
method: "POST",
createUser: "/user"
}
]
...
...exports.invalidUrl = function(req, res) {
var response = [
{
"message": "oops! that is a wrong endpoint, here are the available endpoints "
},
availableEndpoints
]
res.statusCode = 404;
res.setHeader('content-Type', 'Application/json');
res.end(JSON.stringify(response))
}
...
a response message for invalid Url

--

--

--

Solution addict

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create your Analytics Dashboard

XDR Parser indroduction

Customize an Org to Support a New Business Unit in Salesforce

How to use useEffect

PARA+GTD Obsidian setup

[Javascript] var vs let/const

Mod 4: JavaScript Project Build

Trends for JavaScript in 2019

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Moronkeji Ayodeji

Moronkeji Ayodeji

Solution addict

More from Medium

Express Middleware Explained in One Minute

Node.Js for Web Application Development

Software Engineering practices and JavaScript with application frameworks

Top-Image