Node.js HTTP module

28 views Asked by At

Consider the following two codes:

require('http').createServer()
.on('request', (req, res) => { ... })
.listen(3000, () => console.log("Server running") );

and

const http = require('http');
const server = http.createServer((req, res) => { ... });
server.listen(3000, () => console.log("Server running") );

I have the following questions:

  1. Are there benefits in simply running the first code with chaining functions without assigning to constant or is it better to assign the required HTTP module to const http, repectively the Server object to const server? Are the implications regarding how the program will run?

  2. Is it better to put the (req, res) => { ... } request handler inside the on event handler function for REQUEST event or inside the CreateServer function?

0

There are 0 answers