NodeJS基礎(二)

1、動態獲取文件路徑webpack

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())
})

2、中間件web

應用程序級別中間件
     + 萬能匹配(不關心任何請求路徑和請求方法)
    + `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')
            })`
相關文章
相關標籤/搜索