express.js

This is a framework that I often use when doing backend work.

This time, I will be using it in my project, and I will have to explain it to other team members, so I will study and use it again.

express.js is a web framework based on Node.js.

Allows you to easily build http server functions

It provides functions such as routing, middleware, and request/response processing.

What is the routing here? This refers to specifying which function to execute when a request comes in.

What is middleware? The function that operates between request and response is called middleware.

Features of .express.js

The syntax is concise, the middleware structure is good, and the extensibility is good.

Let’s take a look at a simple example code.

const express = require('express')
const app = express()
const port = 3000

app.use((req, res, next) => {
  console.log(`${req.method} ${req.url}`)
  next()
})

app.get('/', (req, res) => {
  res.send('Home page')
})

app.get('/about', (req, res) => {
  res.send('About page')
})

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`)
})

What if you run the code? When accessing http://localhost:3000/, the homepage is displayed

When connecting to http://localhost:3000/about, an introduction page is displayed.

Whenever any route is requested, a log is output to the terminal.

Express.js is very convenient for people who are new to backends because they can simply implement the backend with just these codes.