Start Creating APIs in Node.js with Async/Await

Start Creating APIs in Node.js with Async/Await

Start Creating APIs in Node.js with Async/Await - A sample application to show how we can start creating API in nodejs with Async Await.

Start Creating APIs in Node.js with Async/Await - A sample application to show how we can start creating API in nodejs with Async Await.

Async/await is the lastest and smarter approach to write asynchronous code. It Increases the performance and responsiveness of the application, particularly when you have long-running operations that do not require to block the execution. In this case, you can perform other work while waiting for the result from the long-running task. The big difference is that asynchronous code written with Async/Await looks and behaves like synchronous code. With Async/Await less code is needed to write and it's more readable than callback and promise approach.

Before Async/Await we all mainly used callback and promises

Example of callback

setTimeout(() => {
  console.log('This runs after 1000 milliseconds.');
}, 1000);

Problem with Callbacks- Callback hell

functionOne(()=>{
  functionTwo(()=>{
    functionThree(()=>{
      functionFour(()=>{
        functionFive(()=>{
         --- perform some operations here
    ------------------------------
        })
      })
    })
  })
})

The above one is mainly a situation where callbacks are nested within one other callback for multiple times, And making it difficult to understand and maintain the code.

Example of Promise

Let's have a look a basic promise:

// Return new promise
const promiseFun = new Promise(function(resolve, reject) {
request.get({url: 'url-for-request',headers: {'User-Agent': 'request'}}, function(err, resp, body) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(body));
}
})
})
 
promiseFun.then(function(result) {
console.log(result)
}, function(err) {
console.log(err);
})

In the above code, promiseFunc returns a **Promise **that represents the process of that function. The resolve function signals the **Promise **instance that it has finished.

Afterwards, we can call* .then() *and *.catch() *on that promise function:

then?—?Runs a callback you pass to it when the promise has finished.

catch?—?Runs a callback you pass to it when something went wrong.

Let's Get Started Creating API with Async/Await

Here we will understand to create Basic APIs registration and login with Async/Await .

have a look on the folder structure:

package.json

Have a look inside package.json file.

{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"http": "0.0.0",
"md5": "^2.2.1",
"mongoose": "^5.6.1"
}
}

server.js

 /**
* Module dependencies.
*/
const express = require('express');
const app = express();
const path = require('path');
const server = require('http').Server(app);
const bodyParser = require('body-parser');
const cors = require('cors');
var User = require('./Models/User')
 
const mongoose = require('./Utilities/mongooseConfig')();
 
const authRoute = require('./Routes/auth');
const config = require("./Utilities/config").config;
 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
 
app.use(cors());
 
app.use((err, req, res, next) => {
return res.send({
"statusCode": util.statusCode.ONE,
"statusMessage": util.statusMessage.SOMETHING_WRONG
});
});
 
app.use('/auth', authRoute);
 
// catch 404 and forward to error handler
app.use((req, res, next) => {
next();
});
 
/**
* Start Express server.
*/
server.listen(config.NODE_SERVER_PORT.port, () => {
console.log('app listening on port:' + config.NODE_SERVER_PORT.port);
});

In the code, we can see the npm packages needed, middleware function, routing and server started at the bottom of the file.

** auth.js**

const config = require("../Utilities/config").config;
const UserDAO = require('../DAO/userDAO');
const MD5 = require('md5');
 
/* API to register new user */
let register = async (req, res) => {
if (!req.body.email || !req.body.password) {
res.status(401).json({message:'Parameters are missing'})
} else {
try {
let criteria = {
email: req.body.email
}
const checkEmail = await UserDAO.getUsers(criteria);
if (checkEmail && checkEmail.length==1) {
res.status(401).json({message:'email already registered'})
} else {
let userData = {
firstName: req.body.firstName ? req.body.firstName : "",
lastName: req.body.lastName ? req.body.lastName : "",
email: req.body.email,
phone: req.body.phone,
password: MD5(MD5(req.body.password)),
status: true
};
const addUser = await UserDAO.createUser(userData);
// console
if (addUser) {
res.status(200).json({message:'User registered successfully!'})
} else {
res.status(403).json({message:"Something went wrong"});
}
}
} catch (error) {
res.status(404).json({message:"Something went wrong",error:error});
}
}
};
 
/* API to login user */
let login = async (req, res) => {
if (!req.body.email || !req.body.password) {
res.status(401).json({message:'Parameters are missing'});
} else {
try {
let criteria = {
email: req.body.email,
status: true
};
const checkEmail = await UserDAO.getUsers(criteria);
if (checkEmail) {
let criteria = {
email: req.body.email,
password: MD5(MD5(req.body.password))
};
const checkPassword = await UserDAO.getUsers(criteria);
if (checkPassword && checkPassword.length==1) {
res.status(200).json({message:'Logged in successfully!',result:checkPassword});
} else {
res.status(401).json({message:'Incorrect password'});
}
} else {
res.status(401).json({message:'Email not exist!'});
}
} catch (error) {
res.status(401).json({message:'Something went wrong',error:error});
}
}
};
 
module.exports = {
register: register,
login: login
}

In the above code, there is the main logic of the APIs. APIs are created with Async/Await.

userDAO.js

"use strict";
 
var Models = require("../Models/User");
 
const getUsers = criteria =>
new Promise((resolve, reject) => {
Models.find(criteria)
.then(client => resolve(client))
.catch(err => reject(err));
});
 
const createUser = objToSave =>
new Promise((resolve, reject) => {
new Models(objToSave)
.save()
.then(client => resolve(client))
.catch(err => {reject(err);
console.log(err);
});
});
 
const updateUser = (criteria, dataToSet, options) =>
new Promise((resolve, reject) => {
options.lean = true;
options.new = true;
Models.findOneAndUpdate(criteria, dataToSet, options)
.then(client => resolve(client))
.catch(err => reject(err));
});
 
const deleteUser = criteria =>
new Promise((resolve, reject) => {
Models.findOneAndRemove(criteria)
.exec()
.then(client => resolve(client))
.catch(err => reject(err));
});
 
module.exports = {
updateUser: updateUser,
createUser: createUser,
deleteUser: deleteUser,
getUsers: getUsers
};

In the above file, There are common methods for database related task.

You can download complete code from this page to work the stuff at your end. Once you will complete with all the needed task, API will work like below(see the postman snapshot).

Registration API

Login API

Conclusion

So in this article, I explained how to start Creating APIs in Nodejs with Async/Await. If you are new to Node.js then click here find many demos to start the app for enterprise level application

Top Vue.js Developers in USA

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

We, at HireFullStackDeveloperIndia, implement the right strategic approach to offer a wide variety through customized Vue.js development services to suit your requirements at most competitive prices.

Vue.js is an open-source JavaScript framework that is incredibly progressive and adoptive and majorly used to build a breathtaking user interface. Vue.js is efficient to create advanced web page applications.

Vue.js gets its strength from the flexible JavaScript library to build an enthralling user interface. As the core of Vue.js is concentrated which provides a variety of interactive components for the web and gives real-time implementation. It gives freedom to developers by giving fluidity and eases the integration process with existing projects and other libraries that enables to structure of a highly customizable application.

Vue.js is a scalable framework with a robust in-build stack that can extend itself to operate apps of any proportion. Moreover, vue.js is the best framework to seamlessly create astonishing single-page applications.

Our Vue.js developers have gained tremendous expertise by delivering services to clients worldwide over multiple industries in the area of front-end development. Our adept developers are experts in Vue development and can provide the best value-added user interfaces and web apps.

We assure our clients to have a prime user interface that reaches end-users and target the audience with the exceptional user experience across a variety of devices and platforms. Our expert team of developers serves your business to move ahead on the path of success, where your enterprise can have an advantage over others.

Here are some key benefits that you can avail when you decide to hire vue.js developers in USA from HireFullStackDeveloperIndia:

  • A team of Vue.js developers of your choice
  • 100% guaranteed client satisfaction
  • Integrity and Transparency
  • Free no-obligation quote
  • Portal development solutions
  • Interactive Dashboards over a wide array of devices
  • Vue.js music and video streaming apps
  • Flexible engagement model
  • A free project manager with your team
  • 24*7 communication with your preferred means

If you are looking to hire React Native developers in USA, then choosing HireFullStackDeveloperIndia would be the best as we offer some of the best talents when it comes to Vue.js.