-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dee2e7
commit 6b742f8
Showing
5 changed files
with
59 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('///////////'); |