Nodejs模組
Path
載入 Path 模組1
2
3
4
5
6
7
8
9
10
11
12
13const path = require('path')
// 抓目錄路徑
path.dirname('/xx/yy/zz.js') // 回傳 /xx/yy
// 抓檔案名稱
path.basename('sideproject/test/test.js') // test.js
// 抓副檔案
console.log(path.extname('sideproject/test/test.js')) // .js
// 分析路徑
console.log(path.parse('sideproject/test/test.js'))
// { root: '/', dir: '/sideprojecct/test', base: 'test.js', ext: '.js', name: 'test' }
// 路徑合併
path.join(__dirname,'/xx')
http 模組
載入 Http 模組
1 |
|
使用 Http request 取得 body data 方式
1 |
|
可改寫成 Promise 版本1
2
3
4
5
6
7
8
9
10
11
12const server = http.createServer(async (req, res) => {
const buffers = [];
for await (const chunk of req) {
buffers.push(chunk);
}
const data = Buffer.concat(buffers).toString();
console.log(JSON.parse(data).todo); // 'Buy the milk'
res.end();
})
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!