add "HTTP Server with POST body"
This commit is contained in:
parent
cc3a74468a
commit
e188b52a62
39
tools/server-client/http-server-post-body-node-demo.js
Normal file
39
tools/server-client/http-server-post-body-node-demo.js
Normal 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}`);
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user