Node Web Servers Study is a study in the construction of a basic web server WITHOUT frameworks like express. Suffice to say that implementing express applications is MUCH easier, however taking a little bit of time to study web servers without express provides a great deal of insight into what happens underneath the hood and try to understand the system a little bit more. If you would like to see the express study of this particular project, please take a look.
Here are a few questions examined in this study:
- How do I install the Web Servers Study?
- What are the key takeaways from the Node Web Servers Study?
- How do you install the Web Servers Study?
- What are the key features of the Node Web Servers Study?
- Screenshots
- After cloning this repo, run
npm install
at the command line. - To stat the app, run
node index.js
at the command line. - Visit http://localhost:3000 to see the study live.
-
In order to allow client-server communication via HTTP protocol, you need to use the built-in
http
module. This module provides the interface to create either an HTTP server of HTTP client that can communicate with other clients or servers. Additionally, it should be noted that thehttp
module is a streaming interface.cosnt http = require('HTTP')
-
When a request is made for a particular file on your server, the Mimetype essentially instructs your browser as to how it should be handled. In the case of this study, the Mimetype for the html page to interpret all the files with the html extension to interpret those as HTML document files.
const mimeTypes = { "html": "text/html", // serve html... "js": "text/javascript", // ... and javascript files "css": "text/css", // ... and css files "jpeg": "image/jpeg", // ... and jpeg images "jpg": "image/jpg", // ... and jpg images "png": "image/png", // ... and png images }
-
Express would take the overall win vs raw HTTP Node.js when it comes to creating a http server. Not only does express provide an abstraction layer above the raw http module making it much easier to work with, the benefits of having a wide variety of middleware, etc. vs. raw HTTP Node.js make the process much more simplified and streamlined when developing an app. Also, express has a less"spaghetti"-esque code structure in comparison to express.
To illustrate the type of complexity complexity im refering to, I created two basic servers with a GET route. Both servers do the same thing (i.e. send a response "Hello world!").
"Hello world!" using HTTP server with Node.js:
const http = require('http'); http.createServer(function(request, response) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.write('Hello world!'); res.end(); }).listen(3000); console.log("Your app is listening on Port 3000...");
"Hello world!" using express:
const express = require('express'); const app = express(); app.get('/', function(req, res){ res.send("Hello world!"); }); app.listen(3000, function(){ console.log(`Your app is listening on Port 3000...`); });
Yes! I use extensive Commentary (mostly in the form of my thought process) so that the new and curious can follow the logic in the program and get an idea of my reasoning behind each and every line of code. In addition to my line-by-line commantary, I also provide a Process text file that gives a good outline of my design and implementation process.
Since this study is ongoing, basic functionalities are covered first and more advanced features are added or will be added in the future. I divided this particular study into different branches covering different aspects of basic node servers, which i list below:
Features: | Feature Notes: |
---|---|
basic web server | see nodeWebServer branch |
basic web server with pages | see nodeHttpWebServer branch |
Web server with fully functioning site | see basicServerSite branch |