Mais conteúdo relacionado
Semelhante a node.js dao (20)
node.js dao
- 3. Node.js is a platform built on
Chrome’s JavaScript runtime for
easily building fast, scalable
network applications.
-nodejs.org
- 8. node.js internals
• v8 (yeah, it’s Chrome’s engine)
• libev (event loop)
• libeio (asynchronous version of
POSIX API)
- 9. it is only one thread!
file.read(‘file.txt’,function(data){
//will never fire...
})
while(true){
//blocking entire process
}
- 10. http server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(3000);
- 11. watch file
var fs = require('fs');
fs.watchFile('system.log',function () {
console.log('log changed!');
});
- 14. node & mongo
var mongodb = require('mongodb');
var server = new Server('127.0.0.1', 27017, {});
new Db('test', server, {}).open(function (error, client) {
var collection = new Collection(client, 'tmp');
//insert doc
collection.insert({a:12,b:'string'},{},function(){
//find doc
collection.find({}, {limit:10}).toArray(function(err, docs) {
//do smth with docs
});
});
- 15. node & web sockets
//server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', {hello:'world'});
});
//client
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
});
- 19. do not use for this...
• CPU heavy apps
• data transformation
Notas do Editor
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n