Skip to content

Commit

Permalink
Event build-in module
Browse files Browse the repository at this point in the history
  • Loading branch information
lecaoquochung committed Jul 22, 2016
1 parent 5dee2e7 commit 6b742f8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# nodejs
## GET STARTED
- Hello World
- https://github.com/lecaoquochung/nodejs/commit/ade8f3963a27527a5725c386e96e005f4bbaebf8
- Modules (Define & Use)
- Encapsulate in its own file
### Hello World
- https://github.com/lecaoquochung/nodejs/commit/ade8f3963a27527a5725c386e96e005f4bbaebf8

### Modules (Define & Use)
- Encapsulate in its own file
```
// code.js
exports.project = 'Nodejs';
exports.coding = function() {
console.log('I am coding ' + exports.project);
}
```
-
- fs build-in module (read & write file)
- https://github.com/lecaoquochung/nodejs-example/commit/5dee2e760e010acb15afcaa7ddb3f840b65bba0c

- event build-in module (manage events)
-

## PROJECT ARCHITECTURE
14 changes: 14 additions & 0 deletions example/book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var util = require("util"); // build-in utility module
var events = require("events");
var Class = function() { };

util.inherits(Class, events.EventEmitter);
Class.prototype.ratePoints = 0;
Class.prototype.rate = function(points) {
ratePoints = points;
this.emit('rated');
};
Class.prototype.getPoints = function() {
return ratePoints;
}
module.exports = Class;
10 changes: 10 additions & 0 deletions example/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// event build-in
var events = require('events');
var eventEmitter = new.events.eventEmitter();
var somethingHappen = function() {
console.log('Something happen!');
}

eventEmitter
.on('something-happen', somethingHappen)
.emit('something-happen');
13 changes: 13 additions & 0 deletions example/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var fs = require('fs'); // build-in read & write file

// write file
fs.writeFile('data.txt', 'Hello world!', function (err){
if(err) {throw err;}
console.log('It is saved!');
});

// read file
fs.readFile('data.txt', function(err, data){
if(err) throw err;
console.log(data.toString());
});
23 changes: 12 additions & 11 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// build-in module
var http = require('http');
var fs = require('fs');

// module
// var file = require('./file.js');

http.createServer(function (req,res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n')
}).listen(9000,'127.0.0.1');
console.log('Server is running at http://127.0.0.1:9000');

// write file
fs.writeFile('data.txt', 'Hello world!', function (err){
if(err) {throw err;}
console.log('It is saved!');
});
// event with book.js example
var BookClass = require('./book.js');
var book = new BookClass();
book.on('rated', function() {
console.log('Rated with ' + book.getPoints());
})
book.rate(10);

// read file
fs.readFile('data.txt', function(err, data){
if(err) throw err;
console.log(data.toString());
});
console.log('///////////');

0 comments on commit 6b742f8

Please sign in to comment.