Node.js Is a Javascript Runtime built on google's open source V8 javascript engine. Executes js code outside of the browser. Javascript on the Server: Perfect conditions for using node.js as a web-server We can use js on the server-side of web development build fast, highly scalable network applications (back end) Node.js pros: perfect for building fast and scalable data-intensive apps. javascript across the entire stack, front and back end. npm: huge library of open source packages very active use for: api with database data streaming (youtube) real time chat application server-side web application Modules ----------Filessystem---------- //reading and writing to files const fs = require('fs'); //synchronous way of file reading //takes filepath and char encoding param fs.readFileSync('./txt/input.txt', 'utf-8'); console.log(textIn); //synchronous way of file writing const textOut = `This is what we know about avocado: ${textIn}.` //takes output filepath and content to write fs.writeFileSync('./txt/output.txt', textOut) //asynchronous way of file reading //Non-blocking file execution fs.readFile('input.txt', 'utf-8', (err, data) => { console.log(data); }); console.log('Reading file...'); callback get's called first, starts reading file in background and immediately moves on to next statement, printing console.log. ----------Server---------- const http = require('http'); //creating server, callback will be executed each time a new request hits server const server = http.createServer((req, res) => { res.end('Hello from the server!'); }); //listening for incoming requests server.listen(8000, '127.0.0.1' () => { console.log('Listening to request on port 8000'); }); // ROUTING const url = require('url'); //creating server, callback will be executed each time a new request hits server const server = http.createServer((req, res) => { // ROUTING const pathName = req.url; if(pathName === '/' || pathName === '/overview') { res.end('This is the OVERVIEW'); } else if (pathName === '/product') { res.end('This is the PRODUCT'); } else { //sending a html header element to the browser res.writeHead(404, { 'Content-type': 'text/html', 'my-own-header': 'hello-world' }); res.end('<h1>Page not found!</h1>'); } }); ----------NPM---------- Node Package Manager //USEFUL DEPENDENCIES slugify //used to control url params nodemon //restarts server automatically after code changes //specify npm scripts: { "scripts": { "start": "nodemon index.js" },} Used to manage third-party packages in projects. npm init //creates package.json for projects npm i //installs locally, only works for specific project npm i --global //installing dependencies globally (across projects) npm i *packageName* //installing dependencies for project npm i *packageName* --save-dev //dependencies for development purposes npm run start //run npm scripts npm outdated //check for outdated npm packages npm i *packageName* @1.0.0 //install a specific version of a package "dependencies": { "slugify": "^1.6.6" //only accepts minor and patch releases } "dependencies": { "slugify": "~1.6.6" //only accepts patch releases } "dependencies": { "slugify": "*1.6.6" //accepts all update realeases, might create conflicts } npm uninstall *packageName* //uninstall packages npm install //installs all dependencies in package.json file
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter