Basics Flashcards
Create Server
var http = require(‘http’)
http.createServer(function(req,resp){
resp.writehead(200,{content-type:’text/html’}
resp.write(‘blah’)
resp.end
}).listen(8080)
how to read url
var url = require(url) http.createServer(function(req,resp){ resp.writehead(200,{content-type:'text/html'} resp.write('blah') var urlValue = url.parse(req.url) resp.end }).listen(8080)
How to read files
var fs = require(fs) use fs object to read, edit and delete files
How to raise events
var events = require('events') var eventEmitter = new events.EventEmitter() //subscribe eventEmitter.on('som_event',( data) => { } ) eventEmitter.emit('some random event')
What is Node
Node is a runtime built using the open source Program called V8 Engine, to run Javascript outside the browser.
V8 itself is written in C++ and it is the same program which powers the Chrome Browser. Node Compile Javascript to low level Machine level instructions and runs it.
Some Commans=ds to run in the node command prompt
Global
process
How to export a property from a file in Node
module.export.age = 25
Exports a property named age, with a value 25 from the file
Export a new function named Add
module.exports.add = (one,two) => return one + two
What is nodemon?
something which listens to file changes and restarts Node automatically.Used only in Dev mode.
how to get all command line arguments passed to program
process.argv
Which plugin can be used to process command line arguments
yargs
Which framework is used to serve static files from node
express-static
Commonly used Express MiddleWares
body-parser : This is used to parse the body of requests which have payloads attached to them
cookie-parser: parses Cookie header and populate req.cookies with an object keyed by cookie names
express-session: creates a session middleware with the given options.
How to create an express server
var express = require("express") var app = express() app.get("/",(req,res) => {}) app.listen(8000)
How to add middlewares in express
var express = require("express") var app = express() app.use(MIDDLEWARES) app.get("/",(req,res) => {}) app.listen(8000)