Now we are gonna setup our backend. If you haven’t done step 1or step 2. I would recommend to look at it before doing step 3.

Now lets cd to our server folder outside our src folder.

Then create a controllers folder which hold multiple controllers.

Then cd to controllers folder and create a file for each part of functionality. A user_controller for login, register, logout functionality. A products_controller for crud on products, A cloudinary_controller for uploading images, and a admin_controller for admin.

Now let go to each controller file and have in a module.exports set to an empty object for now, we will add methods later.

Now go to our index.js file in our server and require all modules we installed.

Now lets start importing our middleware for our server.
Which will be body-parser, which initializes the req.body, which therefore allows us to post data. If not used, req.body will become undefined.

Then lets import the express-session which allows our server to use cookie, which therefore allows to manipulate it, and alter it. Which each of our users a unique experience.

If you would like to know about this topic, look at my past article, which has documentation resources and a explanation about express-session, the purpose and ways to explain it.

Then we will use cors as another middleware allow for cross origin requests. No purpose for going deep for this topic, this is pretty minor since this application is going be hosted with a domain of our own.

Next we will import our admin_controller, products_controller, cloudinary_controller, and user_controller file for now. Each serving different purposes.

Next we will import mongoose to connect to our database. Via our connection string, if you have created a mongodb database or having trouble comment below, or contact me on facebook and linkedin or look at my article again.

Finally Let import our server via express.

Now lets set an instance of our server to a variable called app, where all our endpoints will be going through.

Then set the port you want your port to listen to, it can be anything you want over 3500, for simplicity purpose I will set it to 5000.

Now lets connect to our mongodb database by using the connect method which takes a connection string as first argument, then a callback with an err argument catching any errors.

Finally imported we need at the moment, and connected to our mongodb database. Now lets start configuring all of our middleware and endpoints.

Lets starting using app.use() method on our express server, which means that it will be used on everything single endpoint. First lets initialize our req.body, and use the json() method since we will be getting back json data back.

Next lets initialize our express-session, for now we are just setting up a simple session. Make sure to define a SESSION_SECRET in your .env file, so you can use it in your session secret property.

Now lets use cors which allow for cross origin requests.

Now Lets start by setting up our endpoints.

First let add a set timeout which wraps through all our endpoints. Have the time set to 200 milliseconds. So our database will connect before accessing any of the endpoints.

Lets setup our user endpoints which will be adding and removing items to the cart, returning the user data via the cookie, and logging in and out the user.

All these endpoints will be accessing methods on the user_controller file.

Now we will be setting up our products endpoint, which be primarily be used for reading products or product from database. Since only the admin can create, update, delete products.

These 2 endpoints will be using methods in our products_controller file.

For now lets define our admin endpoints, which will create products, update products, and delete products. And read users for now. Our admin we will be able to delete users.

Now we have to just listen to our app on the port we define earlier.

Now that everything is setup in our index file. We can move on to creating our models.

Here is the entire index.js file

Here is the git repository

Next we will setup our controller methods, and mongodb models that will perform our full CRUD

Create Read Update Delete on mongodb database.

Here is the git repo for reference.

For the next step is.

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