2
3
4
5
6
7
8
node
> console.log('Hello World');
Hello World
9
console.log('Hello World');
>node app.js
Hello World
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
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/');
12
13
exports.helloworld = function () {
console.log('Hello World');
}
var test = require('./test.js');
test.helloworld();
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);
15
16
17
{
"name": "TestNodejsApp",
"version": "0.0.0",
"description": "TestNodejsApp",
"private": true,
"main": "app.js",
"author": {
"name": "Qiong Wu",
"email": ""
},
"dependencies": {
"express": "3.4.4",
"jade": "*",
"stylus": "*"
}
}
18
19
20
server.on('connection', function
(stream) {
console.log('someone connected!');
});
server.once('connection', function
(stream) {
console.log('Ah, we have our first
user!');
});
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();
22
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!
});
24

node.js workshop- node.js basics

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    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/');
  • 12.
  • 13.
    13 exports.helloworld = function() { console.log('Hello World'); } var test = require('./test.js'); test.helloworld();
  • 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);
  • 15.
  • 16.
  • 17.
    17 { "name": "TestNodejsApp", "version": "0.0.0", "description":"TestNodejsApp", "private": true, "main": "app.js", "author": { "name": "Qiong Wu", "email": "" }, "dependencies": { "express": "3.4.4", "jade": "*", "stylus": "*" } }
  • 18.
  • 19.
  • 20.
    20 server.on('connection', function (stream) { console.log('someoneconnected!'); }); server.once('connection', function (stream) { console.log('Ah, we have our first user!'); });
  • 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();
  • 22.
  • 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! });
  • 24.