Skip to content

Instantly share code, notes, and snippets.

@likai24
Created October 6, 2017 10:48
Show Gist options
  • Save likai24/e308504c44bd886596fb9146b6c2a720 to your computer and use it in GitHub Desktop.
Save likai24/e308504c44bd886596fb9146b6c2a720 to your computer and use it in GitHub Desktop.
一个简单的测试服务器
var http = require("http");
var server = http.createServer(function(request, response) {
console.log(request.headers);
console.log(request.method);
console.log(request.url);
var body = [];
request.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
console.log('body' + body);
console.log('---------------------------------------------');
// at this point, `body` has the entire request body stored in it as a string
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE \"html\">");
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World Page</title>");
response.write("</head>");
response.write("<body>");
response.write("Hello World!");
response.write("</body>");
response.write("</html>");
response.end();
});
server.listen(8080);
console.log("Server is listening")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment