A chat application (Now.js + Node.js) — 12 lines of code
More and more developers are choosing to build their server-side applications in Node.js / JavaScript. Here are a few very good reasons why you should give Node.js a chance.
- It’s unbelievably fast. Built around Google’s V8 JavaScript Engine, Node scripts manage to blow scripts of pretty much every other interpreted language out of the water. Check out some benchmarks to see what I mean: JavaScript V8 versus PHP, vs. Ruby, vs. Python, and vs. Perl.
- The core code is designed to run asynchronously / in parallel. Most, if not all, of the Node.js standard library can be used asynchronously. This means that all sorts of operations — file reads and writes, database queries and updates, et cetera — can run in parallel, independent of each other. Node.js, therefore, is extremely capable when low response times and high concurrency are necessary.
- The back-end language matches the front-end language. Node.js sites are able to use the JavaScript language in both the front-end and the back-end. This feature has led to all sorts of interesting inventions. For example, I recently discovered Now.js, a library which allows code running in the browser to directly call functions defined in the server code, without any explicit AJAX or JSON requests. I recommend you check out the Now.js site and see for yourself the power of this feature of Node.js.
- Respond to HTTP requests. Obviously, since this is a server, right?
- Send HTTP headers in responses. This is necessary to have the client’s browser parse and display our response correctly.
- Serve a file in the HTTP response. This will demonstrate Node.js’s asynchronous I/O design.
- fs (filesystem), which allows us to perform basic asynchronous file I/O operations
- http, which establishes a basic HTTP server
var fs = require('fs');var http = require('http');var fs = require('fs');var http = require('http');
http.createServer( function(request, response) { // do something here}).listen(8080, '127.0.0.1');var fs = require('fs');var http = require('http');
http.createServer( function(request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello world!');}).listen(8080, '127.0.0.1');- writeHead begins the HTTP response to the client by sending a 200 OK code, along with a Content-Type header that lets the client know that we’ll be sending plain text as a response.
- end sends any last data, closes the response, lets the server know that we’re all done here.
var fs = require('fs');var http = require('http');
http.createServer( function(request, response) { fs.readFile('./response.txt', function(err, data) { if ( err ) throw err; response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(data.toString()); });}).listen(8080, '127.0.0.1');- Download Node.js (not currently available for Windows)
- Prepare a directory for the necessary files.
- Create the files. Use the final server code from above and place it in a JavaScript file. Name it whatever you’d like — server.js, maybe? Create a file called response.txt in the same directory as the server script, and put some text in it, like “Hello world from a text file!”
- Launch the server in your terminal. Change to the directory you created and run node server.js (Change “server.js” to the name you used for your server code.)
- Try it out! Visit 127.0.0.1:8080 in a web browser and you should see the contents of the response.txt file you created appear on the page.