YouTube Lesson by Hassan Algoz.
let http = require('http')
let server = http.createServer(function(req, res) {
// req: methods and properties for request
// res: method and properties for response
// Request & Response:
// HEADER: information about the body, and info about destination, and source.
// BODY (payload): data
// Response Headers
let responseHeaders = {
'Content-Type': 'text/plain'
}
// Status Code:
// 200 -> OK
// 404 -> Not Found
res.writeHead(200, responseHeaders)
// Response Body
res.write("Hello world")
res.end()
})
let port = 8080
server.listen(port, function() {
console.log(`Server listening on port ${port}...`)
})
- Node.js is an open source server framework
- Node.js is free
- Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
- Node.js uses JavaScript on the server
- Node.js is great for real-time services
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
V8 is Google's open source high-performance JavaScript engine, written in C++ and used in Chromium, Node.js and multiple other embedding applications.
npm is the package manager for Node.js, and it is the largest ecosystem of open source libraries in the world. Check out modulecounts.com.
Basically, Node.js enables JavaScript to run on the server/PC rather than the browser. Think of it as an alternative to PHP, C# .NET, and Java on the server.
With just this, node creates an HTTP server:
let http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
In the above code, we require
the built-in module named http
to create an HTTP server. Here, req
refers to the request that comes from the client, and res
refers to the response from the server. We are writing in the headers the status code 200
which means OK, and the content-type. And then, writing and ending the response with 'Hello World!' in the body. Finally, we setup this server to listen on port 8080
for requests.
To run this code, we type in the command-line:
C:/Users/Hassan>node script.js
Now we can visit localhost:8080 to see 'Hello World!' response from the server upon our request.
On the Internet, requests and responses are messages that travel through the medium, be it on wire or wireless as bits (0s and 1s), and they have two main sections, the header and the body. The header contains information such as the IP, Content-Type, Status Code, Request Method, other data and metadata (information) about the body. The body which sometimes is called the payload, is the data you are sending with your request to the server, or the data the the server is sending to you with its response.
For example, when you visit https://www.w3schools.com/nodejs in the browser. By default, the browser sends a request with headers containing the method GET with an empty body, since it only wants to get the page from the server on this URL.
Modules, like the http
module, are simply a collection of functions.
Node.js' built-in modules are written by default to be asynchronous.
exports.myDateTime = function() {
return Date();
}
Use the exports
keyword to make properties and methods available outside the module file.
Save the code above in a file called "my-module.js"
Now you can include and use the module in any of your Node.js files.
let http = require('http');
let dt = require('./my-module');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);