1

Hi all, I am new to node.js and express.js

I am having little bit confusion on creating server on node.js and express.js.

In Node.js we use the http module to create a server.

where as in express we don't use any http module, still we are able to create a server. How server is created here ? Is app.get() is creating it ?

I tried to google the difference but couldn't get the right explanation, pls someone help me here or share a link for a document so, I can understand it better.

// creating server using Node.js

var http = require('http');
var fs = require('fs'); 

var htmlData;

fs.readFile('index.html',(err, data)=>{
  if(err) throw err;
    htmlData = data;
});

 
http.createServer(req, res) {
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write(htmlData); //read the file & write the data content
      res.end();
  }).listen(8000,()=>{console.log("PORT is 8000")}); 




// creating server using Express.js

const express = require('express');
const fs =require('fs');
const app = express();

let htmlData;

fs.readFile('index.html','utf-8',(err,data)=>{
    htmlData = data;
})

app.get('/',(req,resp)=>{
    resp.writeHead(200,{'content-type':'text/html'}).write(htmlData).end();
}).listen(8000);
1

5 Answers 5

3

express.js is built on top of Node.js and uses Node's networking and web framework behind the scenes.

express.js is structured to use "middlewares" which are modules of functionality (basically functions) that process some input and changes state/adds functionality.

For examples there are middlewares specifically to handle http requests received by Node.js before they are passed to your application.

See https://expressjs.com/en/resources/middleware.html and http://expressjs.com/en/resources/middleware/body-parser.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thx, I got it so, behind the scene it is using the http module to create the server.
2

Express is just a library for node js. It uses http module of node to create a server. You call app.use, but this function does a lot of stuff, including http.createServer. Apart from server, express uses middlewares, extending another library called connect. If any method of express is not explained in express documentation, read docs for connect.

If you are learning node now use http, because express provides too much features and does a lot of work instead of you, not allowing to fully understand what is happening

1 Comment

Thx, I got it so, behind the scene it is using the http module to create the server. thanks for the advise.
1

app.listen create the server on express. In express we don't have to use app.createServer(). we can directly use app.listen(3000). Express makes our life easier.

1 Comment

you are right...Got it..
1

Express Js is built on top of Node Js, it's known as a framework. No one really builds servers on bare node, the same way no one builds websites with bare HTML, CSS and javascript.

Express Js is not the only backend framework, but it's the one with the strongest community. Meaning as far as node js is concerned, it has the most users, support and packages. There are some alternatives such as Nest Js, Koa js and Hapi Js. I suggest you look at least one alternative cause it will give you a better understanding of servers.

Comments

-1

In plain Node.js, you must manually create a server:

const http = require('http');
http.createServer((req, res) => { ... }).listen(8000);

In Express you don’t see the http module because Express creates the HTTP server internally.

Key point:

app.listen() is what creates the actual Node.js server — not app.get()

When you do:

const app = express();
app.listen(8000);

Express internally does something similar to:

const http = require('http');
const server = http.createServer(app);  // `app` is a request handler function
server.listen(8000);
  • app.get() only registers routes.

  • app.listen() wraps http.createServer().

  • Under the hood, Express is still using Node’s built-in HTTP server.

So yes, Express is still using the http module — it’s just abstracted away so you don’t need the boilerplate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.