Show Menu
Cheatography

Nodejs Cheat Sheet Cheat Sheet (DRAFT) by

Everything about node

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Module

Module
module has exports property e.g
module.ex­ports = fn() =>{}
module.ex­ports = {hello, move}
Require
returns module.ex­ports
config
Storing config­uration settings:
default, dev & prod
`confi­g.g­et(­con­fiP­rop­erty)
Node wraps you code with function expression & invokes it, this function expression has these argument:
exports, module, __dirname & __filename
.Now you have access to these argument within your code. You can use these argument e.g
module.ex­ports = hello()
to expose ur module

Express

install
npm i express

app = requir­e('­exp­ress')
web server
app.ge­t('/', (req, res) => {
res.send('hello')
})
app.li­ste­n(3000)
route params
req.params
query params
req.query
middlewares
app.use(body-parser.json())
parse body of req into json:-
app.us­e(e­xpr­ess.js­on())

serves static files:-
app.use(express.static(path
.join(__dirname,'public')))
route handle is a middle­ware.
reques­t-p­roc­ess­ing­-pi­peline: Req -

Request processing pipeline

expres­s.j­son()
will pass request to route handler and route will terminate request by sending response.
 

Built - in modules (mostly used)

path
to handle paths
path = requir­e('­path') 
path.j­oin­(__­dir­name, 'public')
os
provide operating system info

os = requir­e('os')
os.fre­emem()
os.upt­ime()
os.use­rInfo()
http
make Node to act as webserver
server = requir­e('­http') 
server.cr­eat­eSe­rve­r(req, res)=>{})
server.li­ste­n(3000)
process
Node process info - global object
port = proces­s.e­nv.PORT || 3000
events
to handle events
 EventE­mitter = requir­e('­events) 
emitter = new EventE­mit­ter()
emitter.on(eventName, listener)

emitte­r.e­mit­(ev­ent­Nam­e,[­arg­s..])
file system
to handle file system
fs = requir­e('fs') 
fs.rea­dfi­le(­path, callback)
fs.cop­yFi­le(­sou­rce.tx­t,d­est.txt, callback)
set enviro­nment vars:
export PORT = 3000

ewwee

require
rrnrnr­nrn­rnr­nrn­rnr­nrnrnrn

www

w
w

Debugging

 
dbDebugger = requir­e('­deb­ug'­)('­app­:db') 
startD­ebu­gger= requir­e('­deb­ug'­)('­app­:st­art')
 
`
Two names are will create:-
app:db & app:start
. You can replace
consol­e.log
with
dbDeb­­ugger
or
startD­­eb­u­gger
.

Define console output:-
export DEBUG=­app­:start
or
export DEBUG=­app:db
or
export DEBUG=­app­:st­art­,app:db
or
export DEBUG=­app:*
 

NPM

npm init
Create packag­e.json file
alt:
npm init  --yes
npm i 
Install npm package
alt:
npm i --save-dev

npm i --save
npm list
lookup depend­encies & version
alt:
npm list --depth=0
npm outdated
check 4 outdated npm package
npm update
update npm package - only updates minor & patches
npm  un
uninstall npm package
npm login
login in npm registry
npm adduser
create user on npm registry