More Related Content Similar to node.js workshop- node.js basics Similar to node.js workshop- node.js basics (20) node.js workshop- node.js basics10. 10
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
11. 11
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
14. 14
module.exports = function () {
this.name = "test object";
this.color = "red";
this.size = "large";
}
var test = require('./test.js');
var testObject = new test();
console.log('name:' + testObject.name);
console.log('color:' + testObject.color);
console.log('size:' + testObject.size);
21. 21
function Test(colour) {
this.colour = colour;
events.EventEmitter.call(this);
this.sendEvent = function()
{
this.emit('EventSent');
}
}
Test.prototype.__proto__ = events.EventEmitter.prototype;
var testObject = new Test('white');
testObject.on('EventSent', function() {
console.log('Event received');
});
testObject.sendEvent();
23. 23
// write 'hello, ' and then end with 'world!'
http.createServer(function (req, res) {
res.write('hello, ');
res.end('world!');
// writing more now is not allowed!
});