Now we have to create a server where browsers and API calls can connect. We can do that with Express' listen method:

app.listen(3000, function() {
console.log('listening on 3000');
});

You can test your server by running node index.js. You should see this:

Adding our First Routes

Now let's create a route that we can access in the browser. Browsers only serve pages using a GET request, so we need to create a route using Express' get method like this:

app.get('/', function(req, res) {
res.send("Yep it's working");
});

You'll need to restart your Node server to see this update. You can do this by typing CTRL + C to stop your server and then run node index.js. If you visit http://localhost:3000 on your web browser, you should see this:

You may have noticed I'm now using an arrow function, which is an ES6 feature. You can learn more about the ES6 specification here.

Now that we've learned how to create a basic route in Express, let's go further by learning CRUD operations using MongoDB. But before we move forward, aren't you tired of restarting your server every time you make a change? You can fix this problem by using a package called Nodemon.

Adding Nodemon

You can install Nodemon by running npm install nodemon --save-dev. Then edit your package.json like this:

Now run npm run dev in your terminal, and you should see:

Now with Nodemon set up, you won't have to restart your server whenever you make a change. However, you will need to refresh your browser to see those changes.

CRUD Operations

CRUD (create, read, update, and delete) operations are the four basic database functions. For this tutorial, we'll use a remote MongoDB service called mLab. We'll be building a simple application to store and retrieve first_name and last_name values from our database.

Setting up MongoDB and mLab

To create a new account and set up a database, you will need to go here. I've provided the images below as a reference guide on setting up your database.

create a new MongoDB Depoyment

select Amazon which is the free tier provider

select a region where you want your database to be hosted (choose one close to where you live for better performance)

specify a name for your database (I named mine "crud")

confirm your specifications/order

go to the Users tab to create a new Database user

when you're done, the created user should pop up

mLab should give you your credentials, which can be found at the top

replace <dbuser> and <dbpassword> with the credentials of the user you just created

With our remmote database set up, we can install MongoDB in our application by running:

Earlier we learned that a browser gets pages via the GET request, but to add data to a database we will need to use the POST request. We can use a service called postman to send these requests.

For our application to receive post requests, we will need a package called body-parser, which we can install by running npm install body-parser --save. Then we can import it into our application like this: