Your First API With Node.js and Express

Alpesh Patel
Jun 18 · 5 min read

This tutorial is a quick and simple guide to Node.js, the Express framework focusing on fundamentals REST routes.

In this tutorial, we’ll set up a server for our API to live on. You can build an API with any programming language and server software, but we will use Node.js, which is the back-end implementation of JavaScript, and Express, a popular, minimal framework for Node.

Installation

Our first prerequisite is making sure Node.js and npm are installed globally on the computer. If you don’t have Node installed , download from here. Once installation is done verify your installation with below commands.

npm -v & node -v

We can test both using the -v flag, which will display the version. Open up your command prompt and type the following.

C:\Users\Admin>npm -v
6.14.11

Your versions may be slightly different than mine, but as long as both are there, we can get started.

Implementation

Let’s create a project directory called express-api and move to it.

mkdir express-api && cd express-api

Now create any new directory or folder. In a new directory, run npm init.

npm init

npm is the node package manager. It will describe what packages we need to run the server, how to run the server from the cli, and store what version of our code we are developing.

This command will prompt you to answer some questions about the project, which you can choose to fill out or not. Let’s just press “Enter” on every question to accept the defaults.

Once the setup is complete, you’ll have a package.json file that looks like this:

{
“name”: “firstnode”,
“version”: “1.0.1”,
“description”: “this is my first node project”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“author”: “Alpesh Patel”,
“license”: “ISC”
}

Installing Express

Express is a simple router for Node.js. It allows a developer to create simple endpoints quickly. There are many routers for Node.js, but Express is the most popular, with great documentation, and many examples. Express is a minimal and flexible Node.js web application framework that provides myriad of HTTP utility methods and middleware for creating a robust APIs quickly and easily.

Let’s install Express with npm:

npm install express

After running above command, package will be included in the project node_modules folder and express will in included in the dependencies section in package.json.

I also highly recommend installing Nodemon as a dev dependency. It’s a simple little package that automatically restarts your server when files change:

npm install nodemon

You can then replace “test”: “echo \”Error: no test specified\” && exit 1" with below snippet in package.json

// package.json  "scripts": {
"dev": "nodemon server.js"
},

Your complete package.json should look like this:

{
“name”: “firstnode”,
“version”: “1.0.1”,
“description”: “this is my first node project”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“author”: “Alpesh Patel”,
“license”: “ISC”,
“dependencies”: {
“express”: “⁴.17.1”,
“nodemon”: “².0.7”
}
}

Now you can create your index.js file and start building your API.

Let’s do some coding

In package.json file, “index.js” is mentioned as the value of “main” property in the JSON. So, index.js is the main entry file for the project. So, a file with name “index.js” needs to be created. Or you can create any other file but you have replace that main file name in package.json.

Let’s create index.js file in our folder:

Let’s start by requiring all your dependencies in index.js

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

You can specify a port, and start the listening like so:

const port = 8000;
app.listen(port, () => {
console.log('Listening on port ' + port);
});

Now, your index.js file should look like this:

Now if you run “npm run dev” (or node index.js if you didn’t install Nodemon) you should see ‘Listening on port 8000’ in the terminal.

Your server is live. But it’s not whole thing. Let’s fix that:

HTTP routes

For this article we are going to use GET method. so in our index.js we going add GET method like this

app.get('/', (req, res) => res.send('My first Node API!'));

your index.js now look like

Now finally run project with npm run dev

It will show your port is listening. Now open chrome or postman and run localhost:8000 it will show “My first Node API!”

In the above code:

  • express package is imported and assigned to a variable express
  • A new instance of express is created and assigned to variable app
  • port is defined as 8000 for the server. This could be any number.
  • A route is defined as “/”, which will be the default path for the site. Once the default page is loaded in the browser, it should print ‘My first Node API!’.
  • Last line will start the Node server.

Similarly we and other HTTP methods like POST, PUT, DELETE:

Thank you for reading, please let me know your questions, thoughts, or feedback in the comments section. I appreciate your feedback and encouragement.

Keep Learning…. !!!

C# Programming

Learn, Build & Execute

C# Programming

Focuses on ASP.Net | ASP.Net Core | .Net & C# advance design, best practices & experiences to make developers life simple..!!

Alpesh Patel

Written by

Technology Specialist and Full-Stack Developer. Find me on linkedin for more details https://www.linkedin.com/in/alpesh-patel-8b930952

C# Programming

Focuses on ASP.Net | ASP.Net Core | .Net & C# advance design, best practices & experiences to make developers life simple..!!