NodeJS基础(二)

一、动态获取文件路径

var fs = require('fs')
var path = require('path')

// 一般在开发命令行工具的时候,这个设计是必须有用的一个特性
// npm
// webpack
// __dirname 和 __filename 这俩就是专门用来动态获取当前文件以及文件所属目录的绝对路径
fs.readFile(path.join(__dirname, './00-文件路径.js'), function (err, data) {
    if (err) {
        throw err
    }
    console.log(data.toString())
})

二、中间件

应用程序级别中间件
     + 万能匹配(不关心任何请求路径和请求方法)
    + `app.use(function (req, res, next)     {
          console.log('haha')
          next()
        })`
    + 只要是以'/xxx/'开头的:
    + `app.use('/a', function (req, res, next) {
              console.log('/a')
            })`

 - 路由级别中间件
     + get:
     + `app.get('/a', function (req, res, next) {
              console.log('/a')
            })`
    + post:
     + `app.post('/a', function (req, res, next) {
              console.log('/a')
            })`
    + put:
     + `app.put('/a', function (req, res, next) {
              console.log('/a')
            })`

    + delete+ `app.delete('/a', function (req, res, next) {
              console.log('/a')
            })`

猜你喜欢

转载自www.cnblogs.com/xiaoxiaodevlog/p/10704207.html