test node.js

Print

console.log('Hello World');

Read file

// Load the fs (filesystem) module.
var fs = require('fs');// 

// Read the contents of the file into memory.
fs.readFile('example_log.txt', function (err, logData) {
 
// If an error occurred, throwing it will
 // display the exception and kill our app.
 if (err) throw err;
 
// logData is a Buffer, convert to string.
 var text = logData.toString();
 
var results = {};

// Break up the file into lines.
 var lines = text.split('\n');
 
lines.forEach(function(line) {
 var parts = line.split(' ');
 var letter = parts[1];
 var count = parseInt(parts[2]);
 
if(!results[letter]) {
 results[letter] = 0;
 }
 
results[letter] += parseInt(count);
 });
 
console.log(results);
 // { A: 2, B: 14, C: 6 }
});

HTTP Server

var http = require("http");

function onRequest(request, response) {
 response.writeHead(200, {"Content-Type": "text/plain"});
 response.write("Hello World");
 response.end();
}

http.createServer(onRequest).listen(8888);

ref : http://blog.modulus.io/absolute-beginners-guide-to-nodejs

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s