Member-only story
Using Node.js & Express.js to save data to MongoDB Database

The MEAN stack is used to describe development using MongoDB, Express.js, Angular.jS and Node.js. In this tutorial I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.
Before we get started I will describe a few terms that you will frequently hear when creating a MEAN stack application. After that we will start building our example.
CRUD
CRUD is an acronym that means Create, Read, Update and Delete. It is used to describe the process of having your data persisted into a database. In this example you will be providing examples of Creating new data into the database and then Reading the data from the database.
Restful API
A RESTful API is an application program interface that uses HTTP requests to GET, PUT, POST and DELETE data. We will be using an API to define when we add data to our database and when we read from the database.
Creating a Node Application
To get started I would recommend creating a new database that will contain our application. For this demo I am creating a directory called node-demo. After creating the directory you will need to change into that directory.
mkdir node-demo
cd node-demo
Once we are in the directory we will need to create an application and we can do this by running the command
npm init
This will ask you a series of questions. Here are the answers I gave to the prompts.

The first step is to create a file that will contain our code for our Node.js server.
touch app.js
In our app.js we are going to add the following code to build a very simple Node.js Application.
var express = require("express");
var app = express();
var port = 3000…