Toggle Sidebar   Previous Lesson Complete and Continue  

  REST API Application

Let's start building the backend application!

Before anything else, let's create new directories that will hold all the code we create over the entirety of our backend.

For your reference, the hierarchy of the entire project folder will include two main folders (api and admin) that look similar to this:

twitter
|
└─── api
│   └─── rest-api
│
└─── admin
│   └─── admin-panel-website


So first of all, let's remove all folders and files inside our twitter directory.

Make sure you're in the /twitter directory and create /twitter/api and /twitter/admin folder with this command:

mkdir api admin

To make sure we're on track, there should now be two directories inside the /twitter folder: /api and /admin

twitter
|
└─── api
|
└─── admin


After you've created new directories, cd into api:

cd api


We'll start building our REST API using Express framework. To set up a new Node.js application, run the following command in the root (api folder) of your project: "npm init -y". This will create a package.json file and initiate the application.

Then, create a new index.js file that will serve as the entry point to the REST API application:

touch index.js


And add this code to it:

const express = require('express');
const dotenv = require('dotenv');

// Load env variables
dotenv.config({ path: './config/config.env' });

const app = express();

const PORT = process.env.PORT || 3000;

app.listen(PORT, console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`));

This code starts a server and listens on port 3000 for a request. And it's also configured to log a "Server running in development on port 3000" message when it first starts.

Make sure you save the file when you're done editing it.

Notice that we're using the external Express, dotenv NPM package, so let's install these packages. Make sure you're in the api folder and run the command:

npm i express dotenv


We also load variables from config.env file. So let's create it

mkdir config && cd config && touch config.env


To run our server application, some scripts need to be added to the package.json file.

Open the /api/package.json file in your editor and update the "scripts" section to look like the following:

  "scripts": {
    "start": "NODE_ENV=production node index",
    "dev": "NODE_ENV=development nodemon index"
  },

The first "dev" script will run the REST API application in development mode. And the "start" script will run the application in production mode, which we'll use when running the application on the DigitalOcean server we create later in the course.

The NODE_ENV variable will be available to the entire application via the process.env.NODE_ENV value. We'll use this to help determine what environment our code is running in.

At this point, your package.json file should look similar to this:

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "NODE_ENV=development nodemon index.js",
    "start": "NODE_ENV=production node index.js"
  },
  "keywords": [],
  "author": "git_username ",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.3",
    "express": "^4.18.2"
  }
}

You may have noticed that we'll be using Nodemon when running the application in development mode.

In the previous lecture, we learned about the nodemon NPM package. This package automatically detects any changes in the application code and restarts the application for us. Without it, we would have to manually restart the application every time we made a change.

To install the nodemon package, use the following command: "npm i -D nodemon"

npm i -D nodemon


Now we can give the application a test run.

Make sure you're in the root of the /twitter/api project and run this command:

npm run dev


This should output a 'Server running in development mode on port 3000' message in your terminal. To stop the server, press 'Control + C'.

Conclusion

Our REST API is now bootstrapped and ready to be built upon! Brick by brick we go.

In the next section, we'll create and connect to a new MongoDB database.

Complete and Continue