Nodejs Cheat Sheet Cheat Sheet
by digotetso via cheatography.com/58474/cs/15450/
Module Request processing pipeline ewwee
Module module has exports property e.g require rrnrnrnrnrnrnrnrnrnrnrnrn
module.exports = fn() =>{}
module.exports = {hello, move} www
Require returns module.exports w w
config Storing configuration settings:
express.json() will pass request to route
default, dev & prod Debugging
handler and route will terminate request by
`config.get(confiProperty)
sending response. dbDebugger = require('debug')
Node wraps you code with function expression & ('app:db')
invokes it, this function expression has these Built - in modules (mostly used) startDebugger=
argument: exports, module, __dirname & require('debug')('app:start')
path to handle paths
__filename.Now you have access to these
path = require('path') `
argument within your code. You can use these path.join(__dirname,
argument e.g module.exports = hello() to Two names are will create:- app:db &
'public')
expose ur module app:start. You can replace
os provide operating system info console.log with dbDebugger or
os = require('os')
Express startDebugger.
os.freemem()
install npm i express
os.uptime() Define console output:-
app = require('express')
os.userInfo() export DEBUG=app:start or
web server app.get('/', (req, res) => export DEBUG=app:db or
http make Node to act as webserver
{ server = require('http') export DEBUG=app:start,app:db or
res.send('hello') server.createServer(req, export DEBUG=app:*
}) res)=>{})
app.listen(3000) server.listen(3000) NPM
route params req.params process Node process info - global object npm init Create package.json file
query params req.query port = process.env.PORT || alt: npm init --yes
3000
middlewares app.use(body-parser.json()) npm i Install npm package
events to handle events alt: npm i --save-dev
parse body of req into json:-
app.use(express.json()) EventEmitter = npm i --save
serves static files:- require('events)
npm list lookup dependencies &
app.use(express.static(path emitter = new version
.join(__dirname,'public'))) EventEmitter() alt: npm list --depth=0
emitter.on(eventName,
route handle is a middleware. npm check 4 outdated npm
request-processing-pipeline: Req -
listener)
outdated package
emitter.emit(eventName,[a
npm update npm package - only
rgs..])
update updates minor & patches
file to handle file system
npm un uninstall npm package
system fs = require('fs')
fs.readfile(path, npm login in npm registry
callback) login
fs.copyFile(source.txt,des npm create user on npm registry
t.txt, callback) adduser
set environment vars: export PORT = 3000
By digotetso Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/digotetso/ Last updated 11th April, 2018. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com