Setup Basic Server with Express Framework

Vaibhav Sharma
Vanila Blog
Published in
5 min readOct 29, 2017

--

Community of digital makers — Join now

“Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.” — Node.js Official

At the present time, The JavaScript Stack is the most popular stack in web technology. The main advantage of this stack is that it is based on one language that is JavaScript. Node JS is the server side language of this stack. There are many web framework of Node JS like Hapi JS, Sails JS, Meteor. But most famous web framework of Node JS is Express JS.

Fast , unopinionated, minimalist web framework for Node JS — Express Official

Create Server in Express JS :

In this article, we discuss about how to create server in Express JS and how to achieve all the task of which have done by any server like ping any specific port, serve static file, server side routing and many more.

If you want to create an HTTP server in Node JS without the use of any web framework, please read my article Create HTTP Server in Node JS.

This article is breakdown into following milestones :

  1. Create Project for Express JS.
  2. Installing Express JS.
  3. Create Server.
  4. Basic Routing.
  5. Serve Static Files.
  6. Express Generator.

Create Project for Express JS :

For creating the project for Express JS application, create a folder and then open the location of that folder in the command prompt for Windows and in the terminal for Mac and run following command.

npm init

Some of few questions is asked by command prompt and main question is :

entry point :
Create project of Express JS

Note: Entry point is the part of the application from where Express JS application bootstrapped.

Installing Express :

If you want to work on Express JS, you have below things :

  1. Node JS
  2. NPM

By the use of below command you install the express.js into your project . This command is run by npm(node package manager).

npm install express --save

Create Server :

Creating the HTTP server is a very easy task in Express JS. Takes following steps for creating a basic server in Express JS.

  1. Import express in your project by require command like below
const express = require('express');

2. Define the port number where the server listens.

const port = 3000;

3. Create app level object of Express JS

const app = express();

4. At last, write the 3 line of code of listening to the server on 3000 port by below code of snippet.

app.listen(port, function () {
console.log("Server is running on "+ port +" port");
});
Create server in Express JS

5. Last step to run the server is

node server.js

or

nodemon server.js

The major difference between the node and nodemon command is that if you use node command you manually run the command each time after save or if you use nodemon command it detects the changes automatically after saving any file of the project.

Basic Routing :

Routing is the most common task of any server. Main responsibility of any server is how an application is responding to the client request to specific endpoints like path, URI by specific HTTP methods like get, post, put, delete.

In the server side routing, each route has one route method, one route path or URI and one or more route handler functions.

In Express JS, routing is not a difficult task. Below code of snippet is the example of routing in Express JS.

app.get('/', function (req, res) {
res.send('<h1>Hello World!</h1>')
})
  1. app is an instance of express.
  2. get is the method of HTTP request.
  3. ’/' is the path of routing.
  4. function is the handler function of routing of express.
Basic routing in Express JS
Screenshot of basic route

Serve Static Files :

Serving the static files is another common task of any server. In Express, we can serve static files like images, CSS, JS files by the use of express.static .

express.static(root, [options]);

The root argument takes the root directory from which you serve the static stuffs of the application like images, CSS files, JS files etc.

app.use(express.static('public'));

You can access the static files on browser

http://localhost:3000/images/img_name.extensionname

Express Generator :

All the above task which is related to creating the server and related to the server is easy but it takes time and if you are a beginner to Node JS it takes to much time. So, Express Generator is the solution to this problem.

npm install express-generator -g

By the above command, the express generator is installed in your system’s global environment. If you want to create an app by express generator use below command.

express myapp

This is my very first article on Express JS. Hope this is informative. Now I’m working on my next article “Send Email in Node JS By NodeMailer”. I’m coming with this article as soon as possible.

Community of digital makers — Join now

--

--