Professional Documents
Culture Documents
Nodejs
Nodejs
• Scope
– Vizibilitatea unei functii sau variabile in diferite parti ale codului in timpul executiei
– Local and global scope
var name = 'Todd';
var scope1 = function () {
// name is available here
var scope2 = function () {
// name is available here too
var scope3 = function () {
// name is also available here!
};
};
};
• Callback
– “A reference to executable code, or a piece of executable code, that is passed
as an argument to other code.”
//exemplu de cod
$('#element').fadeIn('slow', function() {
// callback function
});
//alt exemplu
var callback = function () { console.log(‘123’) };
setTimeout ( callback, 1000 );
– Closures
– Hoisting
– Prototype
Exercitiu:
• Nodejs foloseste V8
• V8 Javascript engine, este creat si optimizat de Google
• A fost creat the Ryan Dahl in 2009
• Open Source
• Traditional I/O
• Non-blocking I/O
db.query("select count(*) from MY_SUPER_BIG_TABLE", function (result) {
doSomethingWithTheResult(result);//wait until the result
});
doSomethingWithoutRsult();//execute without delay
• Instalare
– Download from : https://nodejs.org
• Server HTTP
• Server TCP
• Static file server
• Web Chat Application
Express Underscore
Restify Mongoose
Jade Moment
Socket.io Grunt
Redis
Nodemon
• - Routing – cum o aplicatie treebuie sa raspunda unei cereri, care e formata din path( url) si
din tipul cerere ( GET, POST )
– Exemplu get
app.get('/', function (req, res) {
res.send('Hello World!')
});
– Exemplu delete
app.delete('/', function (req, res) {
res.send('Hello World!')
});
app.route('/book')
.get(function (req, res) {
res.send('Get a random book')
})
.post(function (req, res) {
res.send('Add a book')
})
.put(function (req, res) {
res.send('Update the book')
})