DEV Community

loading...
Cover image for Introduction to Express (Part 1)

Introduction to Express (Part 1)

iamashusahoo profile image Ashutosh ・2 min read

Express is a node framework for creating the backend of the application. A framework is a set of rules that are pre-defined for the smooth development of the application. This helps in making big and stable projects. A framework is different than a library. A library is used for solving small problems. It has a limited number of functionalities. The framework provides a whole set-up for application development. We just need to include our custom requirements.

JavaScript was earlier used as the client-side language for browser interactivity. With the introduction of Node, JavaScript was able to get executed at the CLI and started used for backend development purposes.

There are many frameworks for backend development available on NPM, express is very popular. Being popular and a huge developer community supporting it, it makes things easy for someone who is just starting up, as there are many tutorials available, with community support we will be able to find solutions to our coding problems.

A website generally contains a lot of dynamic pages. To navigate to these dynamic pages a concept routing is used in the express. Let us try to understand routing in express by the help of an example.

//First we install the express framework inside our project folder
C:\Users\user_name\project\FirstExpressProj> npm install express --save

//Then we include express.js in our main file that is generally app.js or index.js

var express = require("express");

var app = express();

//We use the get method to access the required website like  -->> https://localhost:3000
app.get("/", function(req, res){
    res.send("Hi There");
});

// To access the route https://localhost:3000/dog
app.get("/dog",function(req, res){
    res.send("Hi Dogu, How are you doing");
})

// The listen method defines the port on which the server runs, here 3000
app.listen(3000, function(){
    console.log("Server has started!!!");
});

In the above article, we studied the introduction to express. Also, we saw how to install express, include express in our project, and some basic routing with the help of an example. In the next article, we will see, how to include a file, send HTML structure, and some other advanced topics.

Discussion

pic
Editor guide