forked from jimschubert/masteringnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_request.js
More file actions
39 lines (33 loc) · 1.13 KB
/
view_request.js
File metadata and controls
39 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var http = require('http'),
fs = require('fs'),
util = require('util'),
tty = require('tty');
var server = http.createServer(function(req, res) {
console.log("Received a request!");
fs.writeFile((__dirname + '/request.txt'), util.inspect(req) );
res.writeHead(200, { 'Content-Type' : 'text/html' });
res.write("<html><head></head><body><h1>Hello, World!</h1><p>We're serving up html!</p></body></html>");
res.end();
fs.writeFile((__dirname + '/response.txt'), util.inspect(res) );
});
process.on('exit', function(){
if(server) {
console.log('Shutting down the server on http://localhost:9111');
server.close();
}
});
process.on('SIGTERM', function(){
process.exit(1);
});
process.stdin.on('keypress', function(char, key) {
if (key && key.ctrl && key.name == 'c') {
console.log('Caught interrupt signal');
process.exit()
}
});
server.listen(9111);
fs.writeFile((__dirname + '/server.txt'), util.inspect(server));
tty.setRawMode(true);
process.stdin.resume();
console.log("Server is running on http://localhost:9111");
console.log("Hit CTRL+C to shutdown the http server");