node (five) -- express

Basic use of express

Problems using the http module

1. It is troublesome to do different things according to different request paths and processing methods

2. According to the request body and writing the response body is through the flow method, which is troublesome

Common third-party libraries

1.express

​ The ecology is relatively complete

2. koa2

​ Advanced technology, friendly interface is better

manual

Official website: http://expressjs.com

Chinese website (private website): https://www.expressjs.com.cn

2. nodemon

Manual: https://github.com/remy/nodemon#nodemon

1. Automatically restart the server

1. cmd starts directly

npx nodemon index

2. Modify the script field of the package.json file

npm run "配置参数"

2. Configure the nodemon.json file

{
    
    
    "env": {
    
    
      "NODE_ENV": "development"   //开发环境
    },
    "watch": ["*.js", "*.json"],  //修改哪些类型的文件会重启服务
    "ignore": ["package*.json", "nodemon.json", "node_modules", "public"]   //修改哪些文件和文件夹不会重启服务
}

3. express middleware

image-20210329224033828

Middleware processing steps

1. When the request is matched

2. Hand over to the first processing function for processing

3. The function needs to be manually handed over to the subsequent middleware for processing, and the next method is called

processing details

1. If there is no middleware in the follow-up

express found that if the response is not over, express will respond with 404

2. If an error occurs in the middleware

​ will not stop the server

​ Equivalent to calling next(error object)

​ Look for subsequent error handling middleware, if not, respond with 500

3. If the middleware responds, the subsequent middleware still needs to be executed, but it does not respond

4. Commonly used middleware

1.express.static()

​ Handling static servos

2.express.json()

​ Handle the data format of json

3.express.urlencoded()

​ Handle the data format of characters

array related interview questions

1.map

Array.prototype.sx_map = function (callback) {
    const res = []
    for (let i = 0; i < this.length; i++) {
        res.push(callback(this[i], i, this))
    }
    return res
}

console.log(players.sx_map((item, index) => `${item.name}--${item.num}--${index}`))

2.filter

Array.prototype.sx_filter = function (callback) {
    const res = []
    for (let i = 0; i < this.length; i++) {
        callback(this[i], i, this) && res.push(this[i])
    }
    return res
}

console.log(players.sx_filter(item => item.num >= 23))


3.every

Array.prototype.sx_every = function (callback) {
    let flag = true
    for (let i = 0; i < this.length; i++) {
        flag = callback(this[i], i, this)
        if (!flag) break
    }

    return flag
}

console.log(players.sx_every(item => item.num >= 23)) // false
console.log(players.sx_every(item => item.num >= 0)) // true

4.reduce

Array.prototype.sx_reduce = function (callback, initValue) {
    let start = 0, pre
    if (initValue) {
        pre = initValue
    } else {
        pre = this[0]
        start = 1
    }
    for (let i = start; i < this.length; i++) {
        pre = callback(pre, this[i], i, this)
    }
    return pre
}

// 计算所有num相加
const sum = players.sx_reduce((pre, next) => {
    return pre + next.num
}, 0)
console.log(sum) // 85


Guess you like

Origin blog.csdn.net/qq_45256777/article/details/121322234