Reputation: 7330
Socket based example, taken from nodejs website
var server = net.createServer((socket) => {
socket.end('goodbye\n');
}).on('error', (err) => {
// handle errors here
throw err;
});
// grab a random port.
server.listen(() => {
address = server.address();
console.log('opened server on %j', address);
});
Web Server
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
My question is what the difference between the two? Is Web Server something programmed on top of Socket example with additional features like request and response populated with different properties or they are different types of communication protocols? Is socket is what is working under the hood for the Web Server example?
Upvotes: 3
Views: 2767
Reputation: 707876
My question is what the difference between the two?
A web server is a specific type of server that understands the http protocol.
A plain socket server has no particular protocol. You have to create your own meaning/data format for the bytes you send/receive from it.
Both are servers and are listening for incoming TCP connections on a particular port. The difference is in what protocol each is equipped to understand and communicate.
To illustrate some other examples, an ftp server would understand the ftp protocol, a webSocket server would understand the webSocket protocol, an SMTP server would understand the SMTP protocol, an IMAP server would understand the IMAP protocol and so on...
Is Web Server something programmed on top of Socket example with additional features like request and response populated with different properties or they are different types of communication protocols?
Yes, a web server is built on top of a socket and understands data sent on that socket using the http protocol.
Is socket is what is working under the hood for the Web Server example?
Yes.
Upvotes: 9