diff --git a/tools/server-client/http-server-post-body-node-demo.js b/tools/server-client/http-server-post-body-node-demo.js new file mode 100644 index 0000000..e11c781 --- /dev/null +++ b/tools/server-client/http-server-post-body-node-demo.js @@ -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}`); +}) \ No newline at end of file