Tuesday, 27 September 2016

Learning Node.js

Node.js has always looked interesting to me, yet I have never explored it properly.

Every time I hear people mention Node.js its almost always followed by the words "web-server", so naturally I assumed Node.jswas a light weight web-server. Turns out (by just Googling node.js) that it is in fact just a JavaScript Run-time.

Once I installed Node I realised I could just run the following in a console:

$ node
> console.log('Hello');

Undefined is returned because node returns the return value of each command. This command has no return value. 

The reason people always mention web-servers is because its incredibly easy to create a basic web-server with node:


var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');

console.log('Server running on port 8080.');

Now running node web-server.js will launch the web-server, going to http://localhost:8080 will show the text.

Node.js has a very large number of library's which can be used to do almost anything. One project I am quite keen on is a drag and drop page which will parse a CSV file and output relevant data. I'm going to be working on this over the next few hours and I will update this post once complete.

No comments:

Post a Comment