-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleServer.js
More file actions
32 lines (24 loc) · 816 Bytes
/
simpleServer.js
File metadata and controls
32 lines (24 loc) · 816 Bytes
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
//import the http module
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
//Create server and pass listenerFunction
const server = http.createServer( (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
//tell browser reponse is finished and send body
res.end('Hello World');
//start the shutdown process by sending the terminate signal
process.kill(process.pid, 'SIGTERM');
});
//tell server to listen at the hostname:port socket.
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
//handle the terminate signal
process.on('SIGTERM', () => {
//close the last open resources
server.close(() => {
console.log('Process terminated');
})
});