How to parse command line arguments in nodejs?
>node test.js hi hello cool
test,js
var cmdArgs = process.argv.slice(2);
console.log(cmdArgs); //prints ["hi","hello","cool"]
How to write a reusable module and how to import it in test.js?
myreusablemodule.js
module.exports = {
sayHello : function(){
console.log("Hello, how u doing?");
}
};
test.js
var rModule = require("./myreusablemodule"); //returns whatever is assigned to module.exports
console.log(rModule.sayHello());
>node test.js
Explain about event driven programming in nodejs?
In an event driven programming, there is a main loop that listens for events, and then triggers a callback function when one of these events is detected.
Read more here: https://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm
Give an example of event driven programming in nodejs?
All you need to remember is on and emit.
test.js
var events = require("events");
var eventsEmitter = new events.EventEmitter();
var mycustomeventassocfunction = function(){
console.log("i am the response from mycustomeventassocfunction");
};
eventsEmitter.on("mycustomevent",mycustomeventassocfunction);
eventsEmitter.emit("mycustomevent");
>node test.js
How does the async functions work in nodejs?
Events and callbacks help nodejs to support concurrency.
database.query('something', function(err, result) {
if (err) handle(err);
doSomething(result);
});
The async function query takes callback function as the last parameter and the callback function takes err as the first parameter.
Read more here: https://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm
What are the global objects in nodejs?
You don't have to require/include the global objects in nodejs programs. For example, console, process, __filename, __dirname, etc.
Read more here: https://www.tutorialspoint.com/nodejs/nodejs_global_objects.htm
test.js
console.log(__filename);
>node test.js
Sunday, September 18, 2016
Friday, September 9, 2016
Cross-Origin Resource Sharing (CORS)
- Say, web site a.com has some data that b.com site wants to access.
- Web browser's same origin policy won't allow that happen.
- If a.com supports CORS requests (that are made by sites like b.com) by adding a few special response headers, b.com can access the data.
- CORS (Cross-Origin Resource Sharing) is a W3C spec that allows cross-browser communication from the browser. By building on top of the XMLHttpRequest object, CORS allows developers to work with the same idioms as same-domain requests. Read more in this html5rocks tutorial by Monsur Hossain!
Thursday, September 1, 2016
JavaScript unit testing with Mocha, Chai
Google search keyword: sinon js vs jasmine
Some of the interesting sites where testing was discussed with simple examples:
http://sinonjs.org/
https://facebook.github.io/ jest/
http://thejsguy.com/2015/01/ 12/jasmine-vs-mocha-chai-and- sinon.html
https://medium.com/powtoon- engineering/a-complete-guide- to-testing-javascript-in-2017- a217b4cd5a2a
http://blog.testdouble.com/ posts/2016-03-13-testdouble- vs-sinon
Started looking for an example of JavaScript unit testing and came across this site, https://www.sitepoint.com/unit-test-javascript-mocha-chai/.
Simple explanation, give it a try!
Some of the interesting sites where testing was discussed with simple examples:
http://sinonjs.org/
https://facebook.github.io/
http://thejsguy.com/2015/01/
https://medium.com/powtoon-
http://blog.testdouble.com/
Started looking for an example of JavaScript unit testing and came across this site, https://www.sitepoint.com/unit-test-javascript-mocha-chai/.
Simple explanation, give it a try!
Subscribe to:
Posts (Atom)