1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

add "HTTP Server with POST body"

This commit is contained in:
程序员小墨 2023-08-01 22:14:52 +08:00
parent cc3a74468a
commit e188b52a62

View File

@ -0,0 +1,39 @@
/**
* HTTP Server with POST body (node demo)
* CommonJS
*/
const http = require('http');
const url = require('url');
const fs = require('fs')
const port = 8080;
const server = http.createServer((request, response) => {
console.log("\n\n\n");
console.log("当前时间:" + Date.now() + " | " + new Date().toLocaleString() + " | " + new Date().toISOString());
console.log("request.method", request.method);
let requestUrl = url.parse(request.url, true);
console.log("request.url", requestUrl.pathname, requestUrl.query);
console.log("request.headers", request.headers);
var bufferArr = [];
// data - 有一段数据到达触发(多次)
request.on('data', function (data) {
bufferArr.push(data);
});
// end - 数据全部到达触发(一次)
request.on('end', function () {
var postData = Buffer.concat(bufferArr)
console.log("postData", postData)
fs.writeFileSync('postData.dat', postData)
});
response.setHeader("Content-Type", "text/json;charset=utf-8");
response.write("hello!");
response.end();
})
server.listen(port, (error) => {
console.log(error);
console.log(`server is running at port ${port}`);
})