The http Module
The http module is the built-in Node module for the web. It gives Node the tools to send and answer web requests.
Load the http module
This line loads the module into a variable.
const http = require('http');Check what it holds
This line prints the kind of value the module is.
console.log(typeof http);Example
const http = require('http');
const methods = http.METHODS;
console.log(typeof http);
console.log(methods.length);
console.log('http module loaded');Key points
- The http module is built into Node, so no install is needed.
- Load it with require, like any other built-in module.
- The web is the system of pages reached through addresses.
- The http module gives Node tools for working with that system.
- A web server is a program that answers visits to a site.
- One use of the module is building a web server.
- The next lesson builds a server with this module.
In this example, the opening line loads the http module. The second line takes a list of names the module holds. The third line prints object, the kind of value the module is. The fourth line prints 35, the count of names in the list. The last line prints http module loaded.
Note
The http module is for the web only, not for files or folders.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
How do you load the http module into a file?