Serverside for CRUD using MERN stack -Part 1

Sachindu Gimhana
3 min readFeb 20, 2021

This blog is all about the MERN stack. I am going to describe how to develop a simple CRUD application.

The “MERN” phrase refers to the following technologies:

1.MongoDB: MongoDB is a cross-platform document-oriented database program

2.Express.js: is a back end web application framework for Node.js

3.React: is an open-source front end javascript library

4.Node.js: is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser

Install Server with Express and Node

First, we need to create a folder and open that folder through the terminal.

Then run the following command:

npm init -y

After running that command you see something like this:

Here you can see this content in the package.json file.

Install dependencies packages

Now I would like to install some dependencies:

npm install express body-parser cors mongoose

After run above the command you can see like this:

body-parser is an object that exposes various factories to create middlewares.

cors is a node.js package for providing a connect/express middleware that can be used to enable cors with various options

mongoose is a MongoDB object modelling tool designed to work in an asynchronous environment

Then I need to install nodemon that is a utility monitor for any change in your source and automatically restart your server.

npm install nodemon

After running this command you can see like this:

If you don’t want this, you can skip this step.

After that change your package.json as follow:

{"name": "crud","version": "1.0.0","description": "","main": "server.js","scripts": {"start": "node server.js","server": "nodemon server.js"},"keywords": [],"author": "","license": "ISC","dependencies": {"body-parser": "^1.19.0","cors": "^2.8.5","express": "^4.17.1","mongoose": "^5.11.17","nodemon": "^2.0.7"}}

Because of this change, we no need to manually restart the server.

Implement the Server

Now create a new file called server.js in our directory.

Then paste the code following:

const express = require('express');const app = express();const bodyParser = require('body-parser');const cors = require('cors');const PORT = 5058;app.use(cors());app.use(bodyParser.json());app.listen(PORT, function(){console.log("Server is running on port : " + PORT);});

Now run the command

npm run server

You can see your server running on port 5058.

Conclusion

So our server is running successfully. In the next chapter, we are going to set up our database setup with MongoDB.

Stay tuned. Thank you!

--

--