[Node.js] Level 1 new. Intro the Node.js
1.2 Hello You
Let's start with a simple Hello server. Follow the tasks below to create a simple Node server that outputs a greeting.
First, tell the response which status it should have (a successful status is200
).
Next, write a message to the response body in the form of "Hello, this is <your name here>"
.
To finish it up, tell the response to end so the client on the other side knows it has received all the data.
var http = require('http'); http.createServer(function(request, response) { response.writeHead(200); response.write("Hello, this is Zhentian"); response.end(); }).listen(8080);
1.3 Convert Blocking
Not everyone knows why it's important to write non-blocking programs in Node.js. One of these unfortunate souls has written some code to read a file off the file-system using the blocking function readFileSync
. Convert the code to be non-blocking using the readFile
function instead.
Start by changing the call from readFileSync()
to readFile()
.
Next, add a callback method to the readFile()
call. This method should accept error
and contents
parameters.
To finish it up, remove the contents
var declaration, and move the call toconsole.log()
inside your callback.
var fs = require('fs'); fs.readFile('index.html', function(err, contents){ console.log(contents); });
1.4 Running Your Code
While you could go to the website and easily install node, we happen to have a console below where you can practice running node applications.
Go ahead and run that file we just created to read a file off the filesystem withnode file_read.js
$ node file_read.js
1.5 Read File in Server
Now that you know how to create an HTTP server and how to read a file off the filesystem in a non-blocking way, let's try to combine the two.
Instead of just writing a string to the HTTP response, write the contents of index.html
to the response instead.
After response.writeHead(200)
, add a call to fs.readFile()
that readsindex.html
asynchronously. Remember to pass a callback function, that accepts an error
parameter, and a contents
parameter.
Now that you have the file contents, write it to the response
.
To finish up, end the response
after the file contents have been written.
var http = require('http'); var fs = require('fs'); http.createServer(function(request, response) { response.writeHead(200); fs.readFile('index.html', function(error, contents){ response.write(contents); response.end(); }); }).listen(8080);
1.6 Issuing a Request
Let's see our new server in action. We've already run node app.js
, so in the terminal below use curl
to issue a request to http://localhost:8080
and we'll see our server respond with the contents of index.html
.
$ curl http://localhost:8080 --------- <html><p>Hello, this is Dog</p></html>
1.7 Writing Response Headers
Up until now all we've been sending into the response.writeHead()
function is the status code. However, it can take additional parameters.
Consult the node documentation, and add a 'Content-Type' of 'text/html' to the response.
var http = require('http'); var fs = require('fs'); http.createServer(function(request, response) { response.writeHead(200, { 'Content-Type': 'text/html' }); fs.readFile('index.html', function(err, contents) { response.write(contents); response.end(); }); }).listen(8080);
1.8 Response End
Our original Hello server can be shortened since theresponse.end()
function optionally takes data as a parameter. Remove the 'response.write' line altogether, and send the hello string as a parameter on the response.end
function. This will send the data, and once finished add the end to the response.
Instead of passing the content to response.write()
, pass it toresponse.end()
.
var http = require('http'); http.createServer(function(request, response) { response.writeHead(200); response.end("Hello, this is dog"); }).listen(8080);