# Static HTTP server ## Overview ### Time 30 minutes ### Objectives - How to use Node Core HTTP module. - How to bind an HTTP server to a port. - How to accept HTTP requests. - How to respond to incoming HTTP requests. - HTTP Status: 200 OK - With content ## Lab In this lab we will create an HTTP server that responds to all requests with a simple HTML template. 1. Create a new file named ```server.js``` in a directory of your choice. 2. Include the HTTP core module using the ```require(moduleName)``` function and assign the return value to a variable named ```http```. ```JavaScript var http = require('http'); ``` 3. To create a HTTP server execute the ```http.createServer``` ([api doc](http://nodejs.org/api/http.html#http_http_createserver_requestlistener)) function with an anonymous function as an argument and assign it to the ```server``` variable. ```JavaScript var http = require('http'); var server = http.createServer(function () { }); ``` 4. This server is not yet bound to any port. In order to bind to a port, the [server](http://nodejs.org/api/http.html#http_class_http_server) object has the function ```server.listen(port)``` that takes a port as the first argument. ```JavaScript var http = require('http'); var server = http.createServer(function () { }); server.listen(8080); ``` 5. Launch your server at the command line: ```node server.js``` 6. Open your browser and navigate to ```http://localhost:8080``` (replace 8080 with whatever port you chose if different). You will notice that your browser seems to hang and will eventually timeout. This is because our server is not yet doing anything useful with the incoming connection. Let's start by responding to the request with a 200 HTTP status code. Here's where we are so far. ```JavaScript var http = require('http'); var server = http.createServer(function (req, res) { res.statusCode = 200; res.end(); }); server.listen(8080); ``` Notice we have added a few arguments to the anonymous function ```req``` and ```res```. These represent the [request](http://nodejs.org/api/http.html#http_class_http_serverrequest) and [response](http://nodejs.org/api/http.html#http_class_http_serverresponse) streams respectively. A call to ```res.end()``` is required in order to let the client know the server has finished the response. 7. Visit ```http://localhost:8080``` once again. This time there should be a page with no content. We are not here to serve blank pages so let's actually output some content. The response stream (```res``` has a ```write``` function that takes a string to write to the output. ```JavaScript var http = require('http'); var server = http.createServer(function (req, res) { res.statusCode = 200; res.write('Hello World!'); res.end(); }); server.listen(8080); ``` ### Writing the content of a file to the response 1. To load a files content from disk: ```JavaScript var fs = require('fs'); fs.readFile('index.html', function (err, data) { if (!err) { console.log(data); } }); ``` 1. This won't work because theres no ```index.html``` in our directory. Let's create that with something like this: ```HTML