DEV Community

abhick09
abhick09

Posted on • Updated on

Restful CRUD API with Express in NODE.js.

I hope everyone is safe during theses difficult times here i am to today trying to give you some things i have learned on building basic CRUD operations with Express.

As express and node are very powerful things and can be used to built large scale applications.This CRUD results to building restful endpoints which can be used for all your web apps whether it be on the phone via apps or on a domain for the internet.

Alt Text

As we are following the MVC architecture for clean and easily maintainable code so i suggest you to do the same if you are following along.Its pretty simple its is just separating the code into three parts where

Model : Data Schema
View : Your view files
Controller : Where for handling functions for any changes in your schema.

Apart from these some additional folders are needed as your code will tend to become more organized,easy to read and collaborate on as well.We have other folders such as:

Routes: For navigating via different api's for different requests for the same or different schema.
Upload : For our images.
Middleware : For third party configuration such as authentication in my setup. (JWT will be covered in next post)

Lets get started once when you npm init your project as in my previous post.You now create server.js file to run your node server.Where you require a http module which is a default package in NODE and the file is as follows.

Alt Text

Then we should run node server.js in your terminal which will be reflected on localhost:5000 in your browser.

Now creating you first route.Create a route folder and inside it add your file where you want to store your routes in my case which is devRoutes.js.
After that your need to configure your route file as per following.

Alt Text

Once you have configured it you need to add your route in the app.js file where you need to import the route file and then call it using app.use followed by the url you want where app.use('/dev',devRoutes) will take you to getting all the request in that route.As below.

Alt Text

We will be using a non relational database called mongodb.You can configure any other database with this setup.

Now we configure our database and start with the CRUD operations.I suggest that you to use the online db with mongodb atlas which has free for development purpose(simply google mongodb atlas) as they will be very easy to configure and use and you can even put your projects online when you want very less configuration will be needed to configure as compared to shifting from an local environment.

You should now create a model and controller folder. As for now in this project we are dealing with end points and testing it with postman we will not need view aspects of the project but you can add the view and fetch the end points in your node app as well.

We will be using app.js files for all of our configuration needed including our database.As configuring mongo db atlas in pretty simple and easy or you can setup you local mongo db on your device as well.

For online users once you have logged in to the mongodb atlas you need to create a cluster for your database.You should add your admin user name and password as well.

The UI of mongo atlas is pretty comfortable you can find your way around its will be under heading name database security.

Now once you set the user name password you should click on connect and then connect to your application.

Then there you can find a link which will be required to setup mongo to your application.

Lets start our db setup by installing the mongoose package.Where we install npm install mongoose.

You will also need to setup body-parser to access data using json so npm install body-parser and its setup is also needed.

Once you have connected you need to navigate to the app.js file and create a connection.Once everything is configured your app.js will look like.I recommend you need to put your password in a safe env file but for demo i have directly have this there.

Alt Text

Once you have made a connection you need to create a schema.So as to create a schema you need to add models folder in your setup and add a model.js in that folder which should at last look like this or as per your choice of other many data types i would suggest to the official mongodb documentation.

Alt Text

Once the schema is created we need to work on both our routes and controllers where geeting individual data will require id for posting data will require a body and some other functions are needed to be followed as per the files below.Once you create your schema you need to create a controller and a routes file as follows.

router.js

Alt Text

Controller.js

Alt Text

At last you need to test the end points with postman with its requests.

Alt Text

Alt Text

Thanks for making it through this much hope for a feedback just trying to be the part of a community.Next post will be CRUD with image upload and JWT authentication which will guard the routes.

Top comments (0)