Sources Reskilling Program • Node.Js Is an Open Source Server Environment

Sources Reskilling Program • Node.Js Is an Open Source Server Environment

Sources Reskilling Program • Node.js is an open source server environment. • Node.js is free • Node.js allows you to run JavaScript on the server. • Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) • Node.js is written in C++ • Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. 2 Interne Orange Why node ? • Fast, efficient and highly scalabale • Event driven, non blocking I/O model • Popular in industry • Same language on the front and the back end • The MEAN stack ( Mongo Express Angular NodeJS) • Node.js uses asynchronous programming! 3 Interne Orange Used by many modern companies : • Linkedin • Wal-Mart • Paypal • Uber • Netflix 4 Interne Orange 5 Interne Orange Menu 0) Js reviews ? 1) NodeJS 2) Npm 3) Express 4) Socket IO 6 Interne Orange If you have more blocking operations, the event queue gets even worse: If you have more blocking operations, the vent queue gets even worse: https://www.youtube.com/watch?time_continue=2&v=8aGhZQkoFbQ 7 Interne Orange The Event Loop is a queue of callback functions. When an async function executes, the callback function is pushed into the queue. The JavaScript engine doesn't start continue processing the event loop until the code after an async function has executed. 8 Interne Orange https://www.w3schools.com/nodejs/default.asp http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a- 8ef7-0f7ecbdd56cb https://openclassrooms.com/fr/courses/1056721-des-applications-ultra- rapides-avec-node-js/1057364-les-modules-node-js-et-npm https://thinkmobiles.com/blog/node-js-app-examples/ https://www.tutorialsteacher.com/nodejs/nodejs-tutorials Reference Videos : https://www.youtube.com/watch?v=RLtyhwFtXQA&feature=youtu.be https://www.youtube.com/watch?v=pU9Q6oiQNd0&feature=youtu.be https://www.youtube.com/watch?v=fBNz5xF-Kx4 9 Interne Orange • Know client / server protocol • Confortable with JS / ES6 Prerquities • Have already use a framework https://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js 10 Interne Orange Download Node.js from the official Node.js web site: https://nodejs.org 11 Interne Orange 12 Interne Orange Node --version Test 13 Interne Orange Create a folder Create a file and write : console.log('hello world !'); Then execute using node <your filename> Hello world n°1 14 Interne Orange var http = require('http'); http.createServer( function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(8084); Hello world n°2 15 Interne Orange Reminder To stop an application : 16 Interne Orange var http = require('http'); var server = http.createServer( function (req, res) { res.writeHead(200, {"Content-Type": "text/html"}); res.end('<p>Voici un paragraphe <strong>HTML Hello world n°2 </strong> !</p>'); Sending html }); server.listen(8084); 17 Interne Orange var http = require('http'); const PORT = process.env.NODEPORT || 8084 ; // this is a comment var server = http.createServer(function(req, res) { res.writeHead(200, {"Content-Type": "text/html"}); Hello world n°2 res.end('<p>Voici un paragraphe <strong>HTML</strong> !</p>'); Acces variable env }); server.listen(PORT,() => console.log(` Server running on port ${PORT}`)); 18 Interne Orange The fs module provides a lot of very useful functionality to access and interact with the file system. • fs.access(): check if the file exists and Node.js can access it with its permissions • fs.appendFile(): append data to a file. If the file does not exist, it's created • fs.chmod(): change the permissions of a file specified by the filename passed. Related: fs.lchmod(), fs.fchmod() Hello world n°2 • fs.chown(): change the owner and group of a file specified by the filename passed. FS Related: fs.fchown(), fs.lchown() • fs.close(): close a file descriptor (file system) • fs.copyFile(): copies a file • fs.createReadStream(): create a readable file stream • fs.createWriteStream(): create a writable file stream • fs.link(): create a new hard link to a file • fs.mkdir(): create a new folder • fs.mkdtemp(): create a temporary directory 19 Interne Orange • fs.open(): set the file mode • fs.readdir(): read the contents of a directory • fs.readFile(): read the content of a file. Related: fs.read() • fs.readlink(): read the value of a symbolic link • fs.realpath(): resolve relative file path pointers (., ..) to the full path • fs.rename(): rename a file or folder Hello world n°2 • fs.rmdir(): remove a folder FS • fs.stat(): returns the status of the file identified by the filename passed. Related: (file system) fs.fstat(), fs.lstat() • fs.symlink(): create a new symbolic link to a file • fs.truncate(): truncate to the specified length the file identified by the filename passed. Related: fs.ftruncate() • fs.unlink(): remove a file or a symbolic link • fs.unwatchFile(): stop watching for changes on a file • fs.utimes(): change the timestamp of the file identified by the filename passed. Related: fs.futimes() • fs.watchFile(): start watching for changes on a file. Related: fs.watch() • fs.writeFile(): write data to a file. Related: fs.write() 20 Interne Orange const path = require('path'); const fs = require('fs'); // base file name console.log(path.basename( __filename )) //directory name console.log(path.dirname( __filename )) Hello world n°2 FS //file extention (file system) console.log(path.extname( __filename )) //create path object console.log(path.parse(__filename).base) // create a directory fs.mkdir(path.join(__dirname,'test'), {}, err => { if (err) throw err; console.log('Folder created...'); }); 21 Interne Orange var http = require('http'); var fs = require('fs'); http.createServer( function (req, res) { Hello world n°2 FS fs.readFile('demofile1.html', function ( err, data) { (file system) res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); res.end(); }) 22 Interne Orange }).listen(8080); ); 23 Interne Orange What is a Module in Node.js? Consider modules to be the same as JavaScript libraries. A set of functions you want to include in your application. 24 Interne Orange What do we have here? Importing var http = require('http'); a module Callback http.createServer(function (req, res) { function res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); (trigged when }).listen(8080); ready) 25 Interne Orange Include Your Own Module, Step 1 : make it Create a module that returns the current date and time: exports.myDateTime = function () { return Date(); }; Use the exports keyword to make properties and methods available outside the module file. 26 Interne Orange Include Your Own Module Step 2 : use it Use the module "myfirstmodule" in a Node.js file: var http = require('http'); var dt = require('./myfirstmodule'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write("The date and time are currently: " + dt.myDateTime()); res.end(); }).listen(8080); 27 Interne Orange Using argument when running If you have one argument without an index name, like this: node app.js flavio you can access it using const args = process.argv.slice(2) args[0] In this case: node app.js name=flavio args[0] is name=flavio, and you need to parse it. 28 Interne Orange 29 Interne Orange As simple as npm install upper-case 30 Interne Orange Node Package Manager Java - Apache maven Php - composer Python - pip Linux - Apt get Ruby - gem 31 Interne Orange NPM Install 3rd party package ( frameorks, libraires, tools, etc ) Packages get stored in the « node_modules » folder All dependencies are listed in a « package.json » file Npm scripts can be created to run certain tacks such as run a server 32 Interne Orange Node Package Manager Consult website :http://npmjs.org 33 Interne Orange 34 Interne Orange npm config set proxy http://mydomain\username:[email protected]:8181/proxy.pac npm config set https-proxy http://mydomain\username:[email protected]:8181/proxy.pac Set proxy Reminder : http://username:[email protected]:8080 35 Interne Orange Check config with npm get config proxy Change proxy https value from https:// to http:// 36 Interne Orange 37 Interne Orange npm config set registry https://artifactory-iva.si.francetelecom.fr/artifactory/api/npm/npmproxy Using IVA repository npm config set registry https://artifactory-iva.si.francetelecom.fr/artifactory/api/npm/npmproxy 38 Interne Orange package.json package.json Equivalent to pom.xml, composer.json … https://docs.npmjs.com/files/package.json 39 Interne Orange package.json package.json there are lots of things going on here: • name sets the application/package name • version indicates the current version • description is a brief description of the app/package • main set the entry point for the application • private if set to true prevents the app/package to be accidentally published on npm • scripts defines a set of node scripts you can run • dependencies sets a list of npm packages installed as dependencies • devDependencies sets a list of npm packages installed as development dependencies • engines sets which versions of Node this package/app works on • browserslist is used to tell which browsers (and their versions) you want to support All those properties are used by either npm or other tools that we can use. 40 Interne Orange package.json package.json The main property In a Node.js application, it ‘s the entry point when the module is called via a require statement main": "app.js", The repository property The repository property of a package.json is an array that defines where the source code for the module lives. "repository": { "type": "git", "url": "https://github.com/bnb/metaverse.git" } 41 Interne Orange package.json package.json The dependencies property where dependencies - the other modules that this module uses - are defined. The dependencies property takes an object that has the name and version at which each dependency should be used. Tying things back to the version property defined earlier, the version that a module needs is defined.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    107 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us