Node JS Express MongoDB CRUD
By: Nodirbek Ergashev
Tags: Programming, JavaScript, Nodejs, Crud, Mongodb

Hello World

Firstly, create a folder and open it in your favorite text editor. In my case, VS Code.

Type below in terminal, this helps to us create simple package.json file. It stores essential information for our project.

npm init --y

Our task is to create a very basic web page which says ‘Hello World!’. That’s why we need to create a server file. As mention above, we are going to use Express framework for building web pages.

Express is one of the famous minimal web framework for Node.js. We can install Express package by typing this:

npm install — save express

If you noticed that, it created a folder called ‘node_modules’. This folder stores all package files.

Create a file called server.js in your main directory. Let’s code it with simple server.

Sample server

First two lines of code is used for calling express package to create variable called ‘app’.

Next part of code, we asked for a response in main URL which is ‘/’, and waited for GET response. Then we send a simple text to the browser.

Last part, we launched our server by listening to port 3000.

Save this file and launch our server by:

node server.js

Let’s try out in browser and visit: localhost:3000. Yauhhh. We created the first amazing website.

(you can stop the server by pressing CTRL + C)

Connecting database

Let’s install 2 packages before connecting database.

npm install --save mongodb mongoose

There 2 options for database set up

Connect free MongoDB database (free limit ~500 MB) Install local MongoDB app, use it for locally

Both of them good, I 1-option when we are going do deploy app. 2-option for development stage. I used 2-option in the lesson.

Let’s add those code in our server.js file after 3rd line of code:

Connecting MongoDB database

You can change database link if you are connecting to online MongoDB database.

Start your server by typing ‘node server’, if it says ‘Connected to database’, we can move forward.

To Do App

Move on more complex app, we will enter some data to our application and save it for our database. Then we can do CRUD operation which stands for:

C — create

R — read

U — update

D — delete

We have an awesome package called Mongoose. We can implement complex models with less coding.

Let’s create a folder called ‘models’ and file ‘Task.js’ in on it.

After that, our directory looks like:

Sample structure

Let’s discuss our model we are going to implement it. We will have 3 fields for our model:

title — String

body — String

is_active — Boolean

Let’s implement our Task model, open models/Task.js write this down:

You can learn how to implementing model schema by those code.

Types of data: String, Boolean, Number, Date, …

If we add ‘required: true’ in our field, by definition it asks for data is required.

If you need to add default value for your model, you can do it by adding default section.

And do not forget to export your model at the end of the file.

You can also add nested schema and arrays using Mongoose, but for now, let’s keep it simple.

Add Router

Now are adding more routes, it good style to separate route codes from server file. Let’s delete ‘app.get’ function and add this below. You can get full code from repository.

Let’s create router.js file in main folder and add this:

Let’s launch our server to check whether it works or not.

We have simple routes for To Do App

/ — list of tasks

/create — create

/123 — single task by id

/update/123 — update task by id

/delete/123 — delete task by id

Now, we are ready to go implement routes in router.js file:

It is almost ready to use, but we need set up before testing it.

Install ‘ejs’ package for using template engine

npm install ejs --save

And this code in server.js before adding router

And create folder ‘views’ and file ‘create.ejs’ inside. Add this simple form:

And now we can go to ‘localhost:3000/create’

Sample form

It will return raw formatted data. But, we create a beautiful HTML & CSS page and put this data on-page. I implemented rest operation (get task, update, delete) in router file. You can use all code from my repository.

I hope you got the basic idea of creating a Node.js app. If you have any questions/improvements, feel free to contact me by email(mr.nodirbek77@gmail.com).

Original article: link.

My blog: ergashevn.blogspot.com

Rock on!