'use strict';
// CommonJS import syntax
// 'http' is a standard library
var http = require('http');
// pseudo database
var database = require('fake-database');
var server = http.createServer((request, response) => {
// each time a connection is made, this function is called
// decorate response with header
response.writeHead(200, {'Content-Type': 'text/plain'});
// add content and end process
database.getData((err, result) => {
response.end(result);
// The call of `response.end` signals that the callback treatment is done
});
});
server.listen(3000);
console.log('Server listenning on http://localhost:3000');
'use strict';
setTimeout(() => {
console.log('hello');
}, 1000);
console.log('world');
results in
user@host:~$ node helloWorld.js
world
hello
Compared to the same application in Java:
Built almost twice as fast with fewer people
Written in 33% fewer lines of code and 40% fewer files
Double the requests per seconds
35% decrease in the average response time
source: paypal-engineering.com
Node.js reduces development costs !
So if you want to build with stability, security, and structure in mind, i would personally recommend hapijs. On the other hand, if you want to learn everything, build piece by piece, and code closer to node-core, I’d recommend Koa.
Jonathan Ong, Koa developer
Any
questions ?