Create a simple node script that serves a static HTML file without using a framework.
- Setting up Node.js.
- Understanding the relationship between client and server.
- Using the File System module.
- Asynchronous Operations
- Follow the instructions on nodejs.org under "An example: Webserver" to create a simple web server.
- Change the content that is returned to the client with
res.end(...)
to a string of HTML. Request the page again in your browser. What is the output? Is it what you expected? - Change the response header Content-Type from text/plain to text/html. Request the page again in your browser. How did the output change?
-
Create a data.txt file in the same folder as your node script. Add some arbitrary text to this file.
-
Include the File System module using
require
:var fs = require('fs');
-
Call fs.readFileSync to read the contents of the file you just created. readFileSync will return the contents of the file, which you can store in a variable.
-
Change the
res.end
to return the file contents to the client. -
Refresh the page in your browser. You should see the contents of the file written to the page.
The problem with readFileSync
is that it is blocking. In other words, the
server has to sit and wait for the read operation to complete before it can handle any other
operations or requests. Reading from the file system is slow. It may involve accessing
a hard drive or other media. In order to keep the whole server from getting slowed down by
waiting for the operation to complete, we use readFile
(see below).
The difference between readFileSync
and readFile
:
readFileSync
blocks the server while reading the file then returns thecontents of the read file.readFile
accepts a callback function. It immediately returns so that the server can process other requests, and it calls the callback when the file has been read. The contents of the file is passed as an argument to your callback.
**Note: **It is crucial in Node to use asynchronous operations for any potentially slow operations such as reading a file or accessing the database. The performance of node is highly dependent on being able to process other operations and requests while waiting for slow operations to complete. Javascript's callbacks are a perfect paradigm for this event-based communication structure.
- We're going to redo PART III using
fs.readFile
. - Call fs.readFile, passing an anonymous function as a callback which will process the content once it's read.
- Move
res.writeHead
andres.end
inside the callback and changeres.end
to output the contents of the file to the client. - Refresh the page in your browser. You should see the contents of the file written to the page.
**Success! **Now you know how to create your own web server using nodejs, return content to the client, set the content-type of the response, read a file from the file system, and use asynchronous file reading which goes a long way towards leveraging the speed of nodejs!