Getting Started with Node.js RESTful APIs
(Part 1) Environment, Folder Structure and Server Setup
Getting started with building APIs (Application Programming Interface) is not as tedious as many beginners think. In this tutorial, I will work you through a step by step approach in creating a Node.js RESTful API. At the end of this tutorial, You will be able to build a complete set of API that perform the four basic operations of a RESTful API, i.e C -Create, R - Read, U - Update, D - Delete, So grab a cup of coffee, and lets get this done.
Note: This tutorial is divided into 3 different parts, this is to solidify your understanding as a beginner. The part 1 of this tutorial focus solemnly on setting up your environment, packages installation, server setup, and pre-database setup.
For the sake of this tutorial, we will be building an Employee API that allows you to create an employee, view a list of all employees, view a particular employee, update an employee, and delete an employee record.
To get started, Ensure you have the following tools installed on your local machine, if not, quickly install them while you sip the very first round of your coffee.
- Text Editor (Atom, Sublime, e.t.c) I Prefer Atom
- PostMan (This will be used to test our endpoint once we’re done)
- Terminal (iTerm, command prompt, shell e.t.c)
- Node.js (Ensure Node.js is installed) see here if you’re completely new to node
- Mongo DB (Ensure you’ve mongo db installed, thats our database, need help? check this guide out)
Once you’re set, Launch your terminal and let the real game begin
Environment and Folder Structure
Under this phase, we will create the necessary folders and files we will be working with throughout this tutorial.
Step 1: Navigate to wherever you want to create your EmployeeAPI project or create a new folder.
> cd EmployeeAPI OR> mkdir EmployeeAPI && cd EmployeeAPI
Step 2: Create a new Package.json file where all neccessary information required by npm will be stored. To do this, use the gentle soft npm init command that will work you through the entire process.
> npm init
After following through the wizard, you should get something similar to the screenshot above.
Step 3: Lets try and create a structure for our app, such that different folders and files are created to handle different things.
- Create server.js using
- Create the api folder where the api files will be stored using
- Change directory into the newly created api folder using
- Create Employee Model class inside api/model folder using
- Create Employee Controller class inside api/controller folder using
- Create Employee Route class inside api/routes folder using
Step 4: Now that we’ve setup our folder structure, open up the project directory using your text editor. You should get something similar to the screenshot below:
Now, lets setup our server that will handle our API request and response, to do this we will be installing two npm packages, express and nodemon.
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. want to learn more. visit express.js website.
Nodemon is an utility that monitors for any changes in your source files and automatically restart your server. You can read more about it here
Step 1: To install our express and nodemon, type the following command on your terminal:
npm install express --save
npm install --save-dev nodemon
Step 2: Once installation is completed , open up your
server.jsand type or copy and paste the following snippet:
var express = require('express');//express is a framework for node.js web application
var app = express(); //this is an instance of the express class
var port = process.env.PORT || 3000; //connection port to be usedapp.listen(port);console.log(’App listening to client request on port '+port)
Step 3: Once you’re done with that, save it, open up your terminal and type
nodemon server command in your terminal to start the newly created server.
Congratulations!!!! If you get the same screen as the server console above, then you’ve been able to set up your server successfully. If not, don’t panic, gently scroll up and follow through the steps again. We will be waiting for you to join us in the Part 2 series of this tutorial come next week.