Nodejs Rest Api

Lets build simple nodejs app with CRUD database operations

I will be using MongoDB for DB

Please install mongodb and check the server is runing by running mongo demon and mongo server:

Now, lets create a database and a table

I created phani-dev database and developers table and inserted rows.

use above commands to insert

Install the below npm packages

express, morgan, helmet, monk, body-parser, eslint, nodemon, dotenv, joi

express is a nodejs framework

morgan is a middleware logger

helmet is for HTTP header security

monk: Its a tiny layer which provides improvements for MongoDb usage

nodemon: Automatic restarting of our application when any file changes

joi: its a validator

Create an env variable file for our database

Create a schema for our database

Create a database connection

Create middleware for handling errors

Now in our main app lets use all the packages mentioned above and add a developer route

Now, lets create a developer route with CRUD operations

I created for all types of requests here

api/developers – GET – return all developers

api/developers/id – GET – return specific developer details

api/developers – POST – add a new developer

api/developers/id – PUT – update details for an existing developer

api/developers/id – DELETE – delete developer

Now, we are ready to run our application

Get all developers by running: http://localhost/3000/api/developers

Note: port is not specific

You can see statuscode with success response

Lets add a developer

Lets update an existing developer

Now lets see the updated results

I updated by adding “p” to the end

Lets DELETE a record

Now run and see the results. we will see phani b developer deleted

Thats it. hope you enjoyed the article

Code in my Github: https://github.com/pbndru/phani-express-api

Leave a comment