-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrefactorHttpServer.js
More file actions
27 lines (22 loc) · 1.12 KB
/
refactorHttpServer.js
File metadata and controls
27 lines (22 loc) · 1.12 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
var http = require('http');
// 1. Add an event listener on the server variable that listens to the 'request' event.
// The event listener should take a callback function with two arguments, request and response.
var server = http.createServer();
// 2. Move the logic for handling the request from the 'http.createServer()' callback to your new 'request' event listener.
// Remember to remove the 'http.createServer()' callback once the code has been moved.
server.on('request', (req, res) =>{
res.end('hello w0rld');
})
// 3. Add a second 'request' handler to the HTTP server.
server.on('request', (req, res) =>{
console.log('New request coming in...');
})
// 4. From inside of the new handler, log the message "New request coming in..." using console.log().
server.on('connection', (req, res) =>{
console.log('A new connection is made!');
})
// 5. Listen for the 'close' event on the server.
// The event listener should take a callback function that accepts no arguments.
// 6. Inside the 'close' callback, log the message "Closing down the server...".
server.listen(8080);
console.log('Server running at http://localhost:8080');