Express.js

Madusanka Gajadeera
3 min readMay 27, 2021

What is express.js?

Express.js is a Node.js web application framework that is free and open source. It is used to design and create web apps rapidly and simply. Because Express.js simply requires JavaScript, programmers and developers may quickly and easily develop web apps and APIs. Because Express.js is a Node.js framework, the majority of the code is already developed and ready for programmers to use. Express.js allows you to create single-page, multi-page, and hybrid web apps. Express.js is a lightweight server-side framework that aids in the organization of web applications into a more structured MVC architecture.

Why should you express.js?

Express.js supports JavaScript, a widely used, easy-to-learn language that is also extensively supported. If you are already familiar with JavaScript, developing with Express.js will be simple. Express.js makes it simple to create many types of web apps in a short amount of time. For client requests, Express.js provides a basic routing system. It also includes a middleware that is in charge of making decisions in order to offer the right responses to the client’s requests.

The most popular features valued by web application developers are Node.js’ tremendous performance and Express.js’ simplicity of scripting. You may use Express.js to create websites, web applications, and even mobile apps because it is written in JavaScript.

Features of Express.js

· Routing

Express.js is a sophisticated routing mechanism that uses URLs to support the state of a site.

· Middleware

A part of the application that has access to the database, client requests, and other middleware is known as middleware. It is primarily responsible for the orderly structuring of Express.js’ many functionalities.

· Templating

Express.js has templating engines that enable developers to create dynamic content for web pages by creating HTML templates on the server.

· Faster Server-side development

Many widely used capabilities of Node.js are provided in the form of functions that may be utilized anywhere in the application by Express.js. This saves time by eliminating the need to code for several hours.

· Debugging

Debugging is essential for the creation of effective web apps. Express.js makes debugging simple by including a debugging feature that may determine the particular area of the web application that is causing problems.

Installing Express

Create a package.json file for your application with the npm init command.

$ npm init

Install Express in the directory and add it to the list of requirements.

$ npm install express --save

Copy the code from the example below into a file called app.js.

const express = require(‘express’)
const app = express()
const port = 3000
app.get(‘/’, (req, res) => {
res.send(‘Hello World!’)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

Basic Routing

On the homepage, respond with Hello World!:

app.get('/', function (req, res) {
res.send('Hello World!')
})

Respond to a POST request on the application’s home page, the root route (/):

app.post('/', function (req, res) {
res.send('Got a POST request')
})

In response to a PUT request to the /user route, do the following:

app.put('/user', function (req, res) {
res.send('Got a PUT request at /user')
})

In response to a DELETE request to the /user route, do the following:

app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user')
})

--

--