📜  express js npm - Javascript (1)

📅  最后修改于: 2023-12-03 14:41:04.394000             🧑  作者: Mango

Express JS NPM - Javascript

Express JS is a popular Node.js web application framework that allows developers to easily build web applications. It comes with a set of HTTP utility methods and middleware, making it easier to create robust and scalable web applications.


Introduction to NPM

Before we dive into Express JS, we need to discuss NPM. NPM is the default package manager for Node.js. It is a repository of open-source code packages that developers can use to enhance their projects. NPM makes it easy to download, install, and manage these packages.

To install Express JS using NPM, you simply need to run npm install express in your command line interface.


Creating a Basic Express JS Application

To create our first Express JS application, we need to follow a few simple steps:

  1. Initialize a new project with npm init and create a new file called index.js

  2. Install Express JS using NPM with npm install express

  3. In index.js, require Express JS and create a new application instance with const app = express()

  4. Define a route using app.get('/', (req, res) => {}), which sends a response to the client when the root URL is accessed

  5. Start the server using app.listen(PORT, () => {}), where PORT is the port you want to listen on

// Require Express JS
const express = require('express')

// Create a new Express JS application instance
const app = express()

// Define a route that sends a response to the client
app.get('/', (req, res) => {
  res.send('Hello World!')
})

// Start the server
app.listen(3000, () => {
  console.log('Server listening on port 3000!')
})

Middleware in Express JS

Middleware is a way to extend the functionality of Express JS. Middleware functions have access to the request and response objects and can modify them or execute additional logic before the final response is sent.

To implement middleware in Express JS, you can use the app.use() method. This method takes a function as an argument and will execute that function for every request to the server.

// Middleware function that adds a timestamp to the request object
const addTimestamp = (req, res, next) => {
  req.timestamp = new Date()
  next()
}

// Use the middleware function for every request to the server
app.use(addTimestamp)

// Route that sends the timestamp in the response
app.get('/', (req, res) => {
  res.send(`The current time is ${req.timestamp}`)
})

In this example, we define a middleware function that adds a timestamp to the request object. We then use the app.use() method to execute this function for every request to the server. Finally, we define a route that sends the timestamp in the response.


Conclusion

Express JS is a powerful and flexible web application framework that can help you build robust and scalable web applications. With the help of NPM, developers can easily manage their project dependencies and enhance their applications with middleware functions.

Whether you are new to Node.js or an experienced developer, Express JS is definitely worth considering for your next web development project.