-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (19 loc) · 900 Bytes
/
Copy pathindex.js
File metadata and controls
30 lines (19 loc) · 900 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
const http = require('http');
const hostname = 'localhost';
const port = 4000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, my name is Yash!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
process.on('SIGINT', () => {
console.log('Server shutting down...');
server.close(() => {
console.log('Server stopped');
process.exit(0); //Exit the Node.js process
});
});
//With this code, you can now gracefully exit the server by pressing Ctrl + C in the terminal where the server is running. The signal handler captures the SIGINT signal sent when Ctrl + C is pressed and initiates the server shutdown process. The server will close existing connections and then stop before the Node.js process exits.