160 likes | 187 Views
In this tutorial, we will learn about Express.js, Install Express via the Node Package Manager and Create Sample Web server using express.js.
E N D
JS become a ECMA (European Computer Manufacturers Association) standard by 1997. It remains a standard in client- side app overyears. JavaScript created in 1995 by BrendanEich (@Netscape labs). Google V8, chrome, Sep2008 Node createdin2009 by Ryan Dahl using V8 power (c++& javascript) : With Node JS start being as a server-sidelanguage.
Highly scalable web servers for webapplications. Web services (RESTfulAPI) Real-Time apps as its supportsWeb-Sockets. App with queuedinputs. Data streamingapps.
Single-threaded execution with the use of callback functions => not resource- intensive • suitable for high latency I/O operations e.g: databaseaccess • Scalable as it combines Server and application logic in one singleplace
Node is event-driven, it handles all requests asynchronously from on single thread. Aevent loop is used to schedule tasks in the event-drivenprogramming. Node programmingstyle is christened Continous-Passing Style (CPS): Every asynchronous function has at least a Callback function as one of its parameters. This later is call after the asynchronous section isexecuted. var fs =require("fs"); fs.readFile ("foo.txt", "utf8", function(error, data){ if (error){ throwerror; } console.log(data); }); console.log("Reading file...");
Using asyncmodule Using process.nextTick()trick: functionadd(x,y,callback){ setTimeout(function() { callback(x +y); },0); } add(2, 3, console.log); console.log("The sum is:"); function add (x,y,callback) { process.nextTick(function() { callback(x +y); }); } add(2, 3, console.log); console.log("The sum is:");
Non-blocking has always to do with I/O (filesystem, database,…) The following code is asyn but notNon-blocking: function add (x,y,callback) { process.nextTick(function(){ while (1){ callback(x +y); } }); } add(2, 3, console.log); console.log("The sum is:");
Install NodeJS: http://nodejs.org/download/
Create a app.js file with the followingcode: • Start your server by issuing the followingcmd: • > nodeapp.js Nb: NodeJS has got aREPL!
NodeJS extensions are known asmodules • NodeJS is being backed by an active community whichprovides • an overwhelming number ofmodules • NPM command is used to install and manage newmodules: • Ex: • npm installasync • npm installasync@1.0.x • npm install –gasync • npm serach<MODULE_NAME> • npmoutdated • npm update npm–g • npm rmasyn • « require(<MODULE_NAME>)» is used to load a givenmodule
ExpressJS helps to fit web projects to MCV designpattern • To installExpressJS: • > npminstall –g express • Express has got a scafolding capability that help you create asustainable • project structure, easy to extend andmaintain.Express-generator isthe module dedicated to thatjob. • > npminstall –g express-generator
Find a ExpressJS app example on myGITHUB: • https://github.com/ndjido/NodeJS_intro