Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. Pro HTML5 Application Hacks with Nod... > Hack 79. Deliver “Hello Html5” to th...

Hack 79. Deliver “Hello Html5” to the Browser

With this classic “Hello World” hack you’ll be able to use Node.js to handle requests from browsers and respond with content.

Let’s get a handle on our first HTTP request and response with Node.js. We’ll start as simple as possible: we’ll use the example “Hello World” application from nodejs.org.

To get started, we only need to navigate to an empty directory within our filesystem and create an empty file, such as server.js.

We’ll add the following to the file:

var http = require('http');
http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('Hello Html5\n');
}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/'

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint