Home >Web Front-end >JS Tutorial >A brief discussion on how to use Node third-party framework Express

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

青灯夜游
青灯夜游forward
2022-05-23 20:34:411936browse

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:juejin.cn. If there is any infringement, please contact admin@php.cn delete