Develop a backend server for your application using express.

Sharmila S
featurepreneur
Published in
4 min readApr 11, 2021

In this article, I’ll be explaining how you can get started with express to write server-side code.

Node.js —A Javascript framework used for building backend for your applications.

Express — Framework for node.js

npm (node package manager) — package manager for installing necessary modules to work with your application.

First things first, we should have node.js installed in our system. Click here to visit the node.js official site, select the platform that you are using and download.

After installing node.js, create a new folder and open the folder in your favorite editor.

$ mkdir express-demo$ cd express-demo$ code .

Now, let’s create our main application file.

$ touch index.js

Open your terminal at the location of your folder, and type npm init

$ npm init

The above command initializes npm for your project, give yes for the default options. You will see a package.json file created.

package.json file contains information about your application including all the dependencies.

The next step, install express using npm.

$ npm install express

By default, when you install any module using npm, the package.json file gets updated with the name and version of the modules that are installed.

package.json with express listed as a dependency module

Furthermore, you will also see a directory called node_modules being created. This directory holds all the modules that are installed and used in your project.

Let’s write our server code in index.js

index.js
  • Firstly, we need to import express to use it.
const express = require("express");
  • Next, Instantiate express to a variable (generally, we name it ‘app’).
const app = express();
  • Now, let’s specify the port number where your server is going to listen.
app.listen(3000,() => console.log("Server listening at port 3000"));

app.listen() method here takes in two parameters, the first one represents the port number and the other one is a callback function that returns a message to the console, upon successfully listening to the specified port.

  • Now that our server is listening to the port, let’s return some message when the root route(/) is requested from the browser.
app.get("/", (req, res) => {
res.send("Hello World");
});

Using app.get(), you can represent what the server should return for the requested route.

“/” represents the root route.

req -> Represents any data that is sent along with the request.

res -> Used for sending a response back to the browser.

To run your application, use

$ node index.js

The server is running locally in your system, so you send a request to the server using the IP address 127.0.0.1 or localhost, and the port 3000.

or click here -> http://localhost:3000/ or http://127.0.0.1:3000/

To understand the routing better, let’s create another route named ‘/about’ and when about route is requested, return a simple message.

app.get("/about", (req, res) => {
res.send("About route");
});

Now let’s visit our new route, http://localhost:3000/about

Congratulations! Now you have created a simple backend server for your application.

The node_modules directory contains all the dependency modules, which is heavy. But there is no need to include node_modules directory along with your code whenever you move your code elsewhere, especially while uploading to online repositories like a GitHub repository.

The package.json file in the project will contain the details about the dependencies which are required for the project to run, using which one can install the necessary packages anytime.

To avoid uploading certain files/directories to GitHub, a .gitignore file can be used.

‘.gitignore’ is a file that contains a list of names of files/directories in the current repository that you would not like to upload to an online repository.

You can specify the ‘/node_modules’ directory in a .gitignore file to indicate that it should not be pushed to GitHub while uploading the code.

.gitignore file 👇

/node_modules

Later, when you clone any repository from Github or a similar platform, that runs with npm packages, go to the project directory in your terminal and run the command npm i or npm install to download the dependencies listed in the package.json file.

Github repository link for the code — https://github.com/SharmilaS22/medium-express-server

Happy Learning!

If you liked this article, you can support me by buying me a coffee. I’d appreciate your support!

https://www.buymeacoffee.com/sharmilas

--

--

Sharmila S
featurepreneur

Software Engineer | Writes about Full Stack Web Development | 1 X AWS | CKA http://sharmilas.bio.link/