2

Gone through code as below couldnt understand the working of code 1...what is the difference between the two codes below

**

What is the point of using http and express togather in code 1?

Code1

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res)
{
   res.sendFile(__dirname+'/index.html');
});

http.listen(3000, function()
{
  console.log('listening on *:3000');
});

The same thing can be done as

Code2

var express=require('express');

var app=express();

var socket=require('socket.io');

app.get('/',function(req,res){
    res.sendFile(__dirname+'/index.html');

}).listen(8080);
console.log("Listening to port 8080");
Ashish
  • 623
  • 4
  • 10

2 Answers2

0

The app object conventionally denotes the Express application which is created by top level express() function exported by the Express module.

http.listen(): Starts the HTTP server listening for connection

In the second case it works app.listen() which binds and listens for connection on the specified port and it identical to http.listen()

Ashish
  • 623
  • 4
  • 10
0

You're asking about the difference of expressjs own server and http server. They are different in many ways.

Solved here

Ivan Kolyhalov
  • 902
  • 11
  • 16