rudra012/nodeJStutorial
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
Source: http://www.tutorialspoint.com/nodejs/ Node JS tutorial for beginners: This repo contains basic examples for node JS as I started to learn node JS from today from above link. -------------------------------------------------------------------------------------------- Chapter-1: Print Hello Word Installation on UNIX/Linux/Mac OS X, and SunOS: $ sudo apt-get install nodejs To run any js file: $ node js/hello.js -------------------------------------------------------------------------------------------- Chapter-2: basicHTTPserver: -------------------------------------------------------------------------------------------- Chapter-3: REPL Terminal: REPL stands for Read Eval Print Loop Starting REPL $ node > 1 + 3 4 You can use underscore _ to get the last result: > var a = 1 undefined > b = 4 4 > a+b 5 > var sum = _ undefined > console.log(sum);sum; 5 5 > -------------------------------------------------------------------------------------------- Chapter-4: npm: Node Package Manager (npm) $ npm --version 3.10.5 * To update it to the latest version $ sudo npm install npm -g Installing Modules using npm $ npm install <Module Name> eg: $ npm install express var express = require('express'); --> You can use following command to check all the modules installed globally: Here we can find Package.json file path: $ npm ls -g $ npm ls Attributes of Package.json name - name of the package version - version of the package description - description of the package homepage - homepage of the package author - author of the package contributors - name of the contributors to the package dependencies - list of dependencies. npm automatically installs all the dependencies mentioned here in the node_module folder of the package. repository - repository type and url of the package main - entry point of the package keywords - keywords * Updating a module $ npm update express * Search a module $ npm search express * Uninstalling a module $ npm uninstall express * Create a module $ npm init $ npm adduser To publish module: $ npm publish If everything is fine with your module, then it will be published in the reporsitory and will be accessible to install using npm like any other other Node.js module. -------------------------------------------------------------------------------------------- Chapter-5: Callbacks Concept: Callback is nothing just to tell nodeJS that please keep continue execution if blocking task come and when blocking task will done call that function back. Example are in js/Callbackdemo directory $ node js/Callbackdemo/blockingRead.js $ node js/Callbackdemo/asyncRead.js -------------------------------------------------------------------------------------------- Chapter-6: Events Loop: This is same concepts as async calls. The functions which listens to events acts as Observers. Whenever an event gets fired, its listener function starts executing. Steps: Import - Create - Bind - Fire // Import events module var events = require('events'); // Create an eventEmitter object var eventEmitter = new events.EventEmitter(); // Bind event and even handler as follows eventEmitter.on('eventName', eventHandler); // Fire an event eventEmitter.emit('eventName'); Execute Events Example: $ node js/eventLoop.js -------------------------------------------------------------------------------------------- Chapter-7: Event Emitter: EventEmitter provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event. This is same as publish and subscribe feature. If listener is bind to and event means is subscribed that event. It will get execute on respective event emit. Here execution of events based on bind sequence. For example: $ node js/basicEventEmmiter.js -------------------------------------------------------------------------------------------- Author: Shailesh Rudra Ahmedabad, Gujarat email : [email protected]