DEV Community

Cover image for Create a simple Server using Express.js.
Marvie Shoyoye
Marvie Shoyoye

Posted on

Create a simple Server using Express.js.

Introduction.

Express.js is a back end web application framework that is used to build restful APIs with Node js . It is designed for building web applications and APIs. It is a standard server framework for Node js. It is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features.

Prerequisite.

  • Download any code editor e.g. VS code. Visual Studio code which is a code editor with support for development operations like debugging, task running, and version control. Go to https://code.visualstudio.com

vs code installation

  • Download and install the Node.js application, which is the environment in which express.js will run. Got to https://node.js.org

Nodejs installation

STEP 1: Create a folder in a terminal.

Once Node.js is installed NPM(Node Package Manager) is also installed. Create a Folder in the terminal.

mkdir express_js
Enter fullscreen mode Exit fullscreen mode
cd express_js
Enter fullscreen mode Exit fullscreen mode

STEP 2: Create a file

We need to create a file and name it app.js.

touch app.js
Enter fullscreen mode Exit fullscreen mode

STEP 3: Install the NPM.

Now, we install the Npm (Node Package Manager) in the terminal using the command below;

npm init -y
Enter fullscreen mode Exit fullscreen mode

PS: The image below explains the folder creation, file creation, installation of npm(node package manager).

folder creation, file creation and installation of npm

"code ." (This command is used to open the folder in your coding environment).
Enter fullscreen mode Exit fullscreen mode

NOTE: The “npm init” command will initialize a project and create the package.json file.

STEP 4: Install Express.js, Nodemon and Morgan

In other to use express.js in the file, express.js has been pre-installed while installing the node.js. To install Express.js use the command below in the terminal.

npm install express
Enter fullscreen mode Exit fullscreen mode

The nodemon npm Module is a module that develop node.js based applications by automatically restarting the node application when file changes in the directory are detected. Nodemon is a Dev Dependency module.
Installing Nodemon is similar to installing express.js, Nodemon has been pre-installed while installing the node.js. To install Nodemon us the command below in the terminal.

npm install nodemon --save-dev
Enter fullscreen mode Exit fullscreen mode

Morgan is another HTTP request logger middleware for Node. js. It simplifies the process of logging requests to your application.
Also, the morgan dependency has been pre-installed while installing node.js. To install Morgan use the command below in the terminal.

npm install morgan
Enter fullscreen mode Exit fullscreen mode

STEP 5: Import Express.js

Import the express file that was created in the package.json file into the app.js file using the JavaScript code below;

 const express = require("express");
Enter fullscreen mode Exit fullscreen mode

STEP 6: Make an Express.js Function

The function defines the express as a function.

const app = express ();
Enter fullscreen mode Exit fullscreen mode

STEP 7: Create a Route

The route helps to determine how an application responds to a client request to a particular endpoint, which is a URL (or path) and a specific HTTP request method (GET, POST, and so on).
Here, we want to send a message to the server "Hello World"

 app.get( "/", (req,res) => {
   res.send("Hello world")
  })
Enter fullscreen mode Exit fullscreen mode

NOTE: req means request, while res means response.

STEP 8: Create a Port Number

The creation of port number helps to specify the port on which we want our app to listen. The port number ranges from 0 - 65000. Now create a port number of your choice, but for this sake of this we will be using "3030"

const port = 3030;
Enter fullscreen mode Exit fullscreen mode

STEP 9: Listen to your server

Listening refers to the process where a server waits for incoming connections from clients.

app.listen(port, ()=>{
console.log (`server is running on http:localhost:${port}`);
 });
Enter fullscreen mode Exit fullscreen mode
**NOTE:** The local host = "127.0.0.1"
Enter fullscreen mode Exit fullscreen mode

javascript

STEP 10: Run the code in your terminal.

Using the commands below you can run the code in your terminal.

node app.js
Enter fullscreen mode Exit fullscreen mode

npm app.js
or

npm start
Enter fullscreen mode Exit fullscreen mode

npm start

NOTE: “npm start” is a run command from your scripts located in your package.json file.

npm run dev
Enter fullscreen mode Exit fullscreen mode

npm run dev

NOTE: "npm run dev" is used to execute specific scripts defined in the package. JSON file, particularly those scripts related to the development environment setup.

STEP 11: Launch the link in the browser.

Using the link that was generated launch it on any browser e.g. goggle chrome, mozilla fire fox, opera, safari etc..

http://localhost:3030
Enter fullscreen mode Exit fullscreen mode

Hello World

Top comments (0)