How the Node.js file server works
Node.js allows you to load pages or applications from your browser using a simple server script.
The script we presented is no different in its work from various scripts published on the official website or in different "textbooks" or pseudo-lessons on Node.
I'm talking about a pseudo-lesson, because programmers tend to make no difference between a textbook that explains how to use a thing and a textbook that studies its work by going through stages and moving from the simplest to the most complex. When a programmer adds comments to a listing, it is a "tutorial." Are we far from it, and the source code itself is written to be easy to understand?
This is especially significant in the case of Node.js, where all demo scripts are written in a recursive form that makes the reader think like a processor, which seems to be the hacker's ultimate goal.
Therefore, I rewrote the page server, breaking the script into elementary functions, which will allow me to better understand how it works. But in fact, my goal is to take the first step towards a more complex script, a locally working tool server that allows you to use web pages as interfaces to executables, binary or not.
1) Create a server
var server = http.createServer(getFilename);
server.listen(1000);
console.log("Server available...");
The createServer parameter is a callback that will be enabled when the browser logs into port 1000 with the filename as a parameter. Entering, for example, in the browser panel:
127.0.0.1:1000/page.html
2) URL analysis and file name transfer to the system
function getFilename(request, response)
{
var urlpath = url.parse(request.url).pathname;
var localpath = path.join(process.cwd(), urlpath);
fs.exists(localpath, function(result) { getFile(result, response, localpath)});
}
This function retrieves the path from the URL and passes this parameter to a second function that reads the file on the local server or computer.
For this, the url and path modules are used.
3) Reading the file
function getFile(exists, response, localpath)
{
if(!exists) return sendError(404, '404 Not Found', response);
fs.readFile(localpath, "binary",
function(err, file){ sendFile(err, file, response);});
}
The file is read using the fs module, which performs normal operations on the file system, with the exception of running programs that require another module. Our sendFile function is called by the readFile module fs.
4) Send Page
function sendFile(err, file, response)
{
if(err) return sendError(500, err, response);
response.writeHead(200);
response.write(file, "binary");
response.end();
}
The contents of the file are sent to the browser that displays it. This is done by a response object created with the server and passed to each function.
5) List of useful modules
http = require("http");
path = require("path");
url = require("url");
fs = require("fs");
The role of each of them was noticed earlier, with the exception of HTTP, which allows you to use the HTTP protocol to exchange data between the server and the browser (or other user agent ).
6) Full
scenario:http = require("http"),
path = require("path"),
url = require("url"),
fs = require("fs");
function sendError(errCode, errString, response)
{
response.writeHead(errCode, {"Content-Type": "text/plain"});
response.write(errString + "\n");
response.end();
return;
}
function sendFile(err, file, response)
{
if(err) return sendError(500, err, response);
response.writeHead(200);
response.write(file, "binary");
response.end();
}
function getFile(exists, response, localpath)
{
if(!exists) return sendError(404, '404 Not Found', response);
fs.readFile(localpath, "binary",
function(err, file){ sendFile(err, file, response);});
}
function getFilename(request, response)
{
var urlpath = url.parse(request.url).pathname; // following domain or IP and port
var localpath = path.join(process.cwd(), urlpath); // if we are at root
fs.exists(localpath, function(result) { getFile(result, response, localpath)});
}
var server = http.createServer(getFilename);
server.listen(1000);
console.log("Server available...");
This base server can be verified by downloading the page.html file contained in the archive. The script source code is server.js.
Formerly: Introduction to Node.js. Description of the system and interest in its use.
The following article shows how to use Node.js to execute PHP scripts locally from a browser.