airide101
airide101

Reputation: 89

Between Node.JS and Express, can someone explain where the Web Server fits in?

I've recently started server-side development using Node JS and Express but i'm getting confused as to how it all works. From my understanding, The web serve stores the website and returns the pages as they're requested from the browser. Apache is a web server and you would use that for a stack like XAMPP. ASP.NET is a framework that uses IIS Web Server and communicates with that.

But with Node, where's the server? Node is runtime environment and is USED to CREATE a server and Express is a web FRAMEWORK to help with server http requests but what/where is the actual web SERVER? Maybe i'm just not understanding web servers or something? Someone please clarify!

Upvotes: 2

Views: 1030

Answers (1)

Jithin Scaria
Jithin Scaria

Reputation: 1309

For Node, we do not need a web server like Apache or a container that sort of, node can listen to a port and act as a server itself,

and express is web application framework for Node which provides set of features to make life easier.

for a vague comparison, if Node is a telephone then Node + express will be a smartphone. - both can do same stuff but latter have more convenient features.

see below two example of creating a server which listen to a port 3000,

In node:

const http = require('http')

const requestHandler = (request, response) => {
    console.log(request.url)
    response.end('Hello Node.js Server!')
}

const server = http.createServer(requestHandler)

server.listen(3000,() => console.log("app started"));

Node + express

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

app.get('/', function (req, res) {
  res.send('Hello express !')
})    

app.listen(3000,() => console.log("app started"));

Both does the same thing, but with express things are easier.

Upvotes: 6

Related Questions