search
HomeWeb Front-endJS TutorialA brief discussion on how to use Node third-party framework Express

This article will take you to learn about the third-party framework ExpressNode, and briefly talk about how to use the Express framework well. I hope it will be helpful to everyone!

A brief discussion on how to use Node third-party framework Express

1.Express framework introduction

  • ##1.Express is developed by

    Nodejs A very heavyweight third-party framework, it is to the NodeJS server what Jquery is to the HTML client.

    • If you don’t even know how to use Express, you are basically embarrassed to tell others that you know NodeJS
  • ##2 .Express official website:
    • www.expressjs.com.cn/
    • expressjs.com/
    • General When we learn a new technology, we always go to the official website documentation to view its API, and then try a lot. Practice makes perfect
  • 3. Express’s github address: https://github.com/expressjs/express
  • The original author of Express, TJ, is very famous in the node community. He has written more than 200 frameworks. Currently, he has handed over Express to a friend for maintenance. , announced that it will no longer maintain the NodeJS framework and switch to the Go language (https://github.com/tj)
  • 4. The Express official website introduces itself like this: Based on Node. js platform, a fast, open and minimalist web development framework.
    • A very important highlight of Express is that it does not change the existing features of nodejs, but expands on it

        In other words, using Express you can use any nodejs native API, or you can use Express’s API
  • 5. The three core functions of Express

      #1. Hosting static resources
    • The nodejs implementation of static server discussed on the second day The function only requires one line of code in express
      ##2. Routing
    • express has its own routing function, making Node server development extremely simple

        express supports chain syntax, which can make the code look more concise
      ==3. Middleware==
    • The core technology and idea of ​​Express, everything is middleware
      • Although middleware is a bit difficult to understand, it is very convenient to use, similar to

        bootstrap plug-in
          .
  • 2. Download express

Download instructions:

npm i express

If your website is very slow, you can use npm config set registry registry.npm.taobao.org/ to increase the speed

     就是淘宝帮你把这个东西下载淘宝的服务器上,然后你在淘宝服务器上下载

A brief discussion on how to use Node third-party framework Express3.Use Express

//1.导入模块
const express = require('express')

//2.创建服务器
/* express() 相当于http模块的http.createServer() */
const app = express()


//3.接收客户端请求
/*(1)express最大的特点就是自带路由功能,我们无需在一个方法中处理所有请求
		* 路由:一个请求路径对应一个方法(函数)
   (2)在express中,每一个请求都是一个单独的方法
 */

app.get('/',(req,res)=>{
    //响应客户端数据

    //express响应数据 send方法:自动帮我们设置好了响应头,无需担心中文乱码问题
    res.send('月下风起')

})

app.get('/heroInfo',(req,res)=>{
    
    res.send({
        name:'张三',
        age:20
    })
})

//4.开启服务器
app.listen(3000,()=>{
    console.log('服务器启动成功')
})
4-Express to respond to client data

//1.导入模块
const express = require('express')

//2.创建服务器
/* express() 相当于http模块的http.createServer() */
const app = express()


//3.接收客户端请求

//文本类型数据
app.get('/',(req,res)=>{
    //响应客户端数据
    res.send('月下风起')
})

//json格式数据
app.get('/info',(req,res)=>{
    //express自动帮我们将js对象转成json响应给客户端
    res.send({
        name:'张三',
        age:20
    })
})

//文件类型数据
app.get('/login',(req,res)=>{
    res.sendFile(__dirname + '/login.html')
})

//4.开启服务器
app.listen(3000,()=>{
    console.log('服务器启动成功')
})
5.Express Hosting static resources

http://expressjs.com/en/starter/static-files.html
//1.导入模块
const express = require('express');

//2.创建服务器
const app = express()

//托管静态资源(相当于我们之前写的静态资源服务器)
/* 
1.当请求路径为/时,express会自动读取www文件夹中的index.html文件响应返回
2.当路径请求为www文件夹中的静态资源,express会自动拼接文件路径并响应返回
*/
app.use(express.static('www'))

//4.开启服务器
app.listen(3000,()=>{
    console.log('success')
})

6. Use of third-party middleware

1. On the Express official website, there are many third-party middleware, which can make our Nodejs development extremely simple
  • Middle It is a plug-in for the front-end of software. After use, it will add members to req or res in express
    2. All third-party framework learning routines are the same
  • 1. Go to the official website and check the documentation

      2. CTRL C and CTRL V
    3. Use of third-party middleware The steps are generally two fixed steps
  • One: Install
      npm i xxxx
    • (copy and paste from the official website)

      Third party Middleware needs to be installed using npm, which can be understood as a special third-party module
      2: Use
    • app.use(xxx)
    • (Official website copy and paste)

body-parse Third-party middleware: parse post request parameters
  • Install body-parser:

    npm install body-parser
    • https://www.npmjs.com/package/body-parser

    • //导入模块
      const express = require('express')
      //创建服务器
      const app = express()
      
      //使用第三方中间件
      /*所有的第三方模块思路都是一样 
          1.进官网,查文档
          2.找examples(使用示例),复制粘贴
              a.安装第三方模块:`npm i body-parser`
              b.使用中间件: arr.use(具体用法请复制粘贴) 
      使用body-parser中间件之后,你的req会增加一个body属性,就是你的post请求参数
      */
      //(1)导入模块
      const bodyParser = require('body-parser')
      // parse application/x-www-form-urlencoded 
      //(2)使用中间件
      app.use(bodyParser.urlencoded({ extended: false }))
      //解析json参数
      app.use(bodyParser.json())
      
      app.post('/abc',(req,res)=>{
          console.log(req.body)
          //告诉客户端我收到的参数
          res.send(req.body)
      })
      
      app.post('/efg',(req,res)=>{
          console.log(req.body)
          //告诉客户端我收到的参数
          res.send(req.body)
      })
      
      //开启服务器
      app.listen(3000, () => {
          console.log('success');
      })
    For more node-related knowledge, please visit:
  • nodejs tutorial
!

The above is the detailed content of A brief discussion on how to use Node third-party framework Express. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools