0% found this document useful (0 votes)
9 views2 pages

Digotetso - Nodejs Cheat Sheet

This Node.js cheat sheet provides essential commands and modules for using Node.js and Express, including how to handle requests, manage configurations, and utilize built-in modules. It also covers NPM commands for package management, such as installing, updating, and uninstalling packages. The document serves as a quick reference for developers working with Node.js and Express applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Digotetso - Nodejs Cheat Sheet

This Node.js cheat sheet provides essential commands and modules for using Node.js and Express, including how to handle requests, manage configurations, and utilize built-in modules. It also covers NPM commands for package management, such as installing, updating, and uninstalling packages. The document serves as a quick reference for developers working with Node.js and Express applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Nodejs Cheat Sheet Cheat Sheet

by digotetso via cheatography.com/58474/cs/15450/

Module Express (cont) Built - in modules (mostly used) (cont)

Module module has exports property e.g modulmiddlewares events


app.use(body-parser.json()) to handle events
e.ex​ports = fn() =>{} module. parse body of req into json:- EventE​mitter = requir​e('​event
ex​ports = {hello, move} app.us​e(e​xpr​ess.js​on()) emitter = new EventE​mit​ter()

Require returns module.ex​ports serves static files:- emitter.on(eventName, listener)


app.use(express.static(path emitte​r.e​mit​(ev​ent​Nam​e,[​ar
config Storing config​uration settings:
.join(__dirname,'public')))
file to handle file system
default, dev & prod
route handle is a middle​ware.
`confi​g.g​et(​con​fiP​rop​ert system fs = requir​e('fs')
y) reques​t-p​roc​ess​ing​-pi​peline: Req - fs.rea​dfi​le(​path, callback)
fs.cop​yFi​le(​sou​rce.tx​t,d​es
Node wraps you code with function
Request processing pipeline lback)
expression & invokes it, this function
expression has these argument: exports, set enviro​nment vars: export PORT = 3
module, __dirname & __filename.N 000
ow you have access to these argument
within your code. You can use these ewwee
argument e.g module.ex​ports = hell require rrnrnr​nrn​rnr​nrn​rnr​nrnrnrn
expres​s.j​son() will pass request to
o() to expose ur module
route handler and route will terminate
www
request by sending response.
Express
w w
install npm i express Built - in modules (mostly used)
app = requir​e('​exp​ress') Debugging
path to handle paths
web server app.ge​t('/', (req, res) => path = requir​e('​path') dbDebugger = requir​e('​deb​ug'​)('​app​
{ path.j​oin​(__​dir​name, 'public')
res.send('hello') startD​ebu​gger= requir​e('​deb​ug'​)('
os provide operating system info
}) :st​art')
os = requir​e('os')
app.li​ste​n(3000)
os.fre​emem() `
route params req.params os.upt​ime()  Two names are will create:- app:db &
query params req.query os.use​rInfo() app:start. You can replace consol​e.l
http make Node to act as webserver og with dbDeb​​ugger or startD​​eb​u
server = requir​e('​http') ​gger.
server.cr​eat​eSe​rve​r(req, res)=
>{})  Define console output:-
server.li​ste​n(3000) export DEBUG=​app​:start or

process Node process info - global object export DEBUG=​app:db or


port = proces​s.e​nv.PORT || export
3000 DEBUG=​app​:st​art​,app:db
or

export DEBUG=​app:*

By digotetso Not published yet. Sponsored by Readable.com


cheatography.com/digotetso/ Last updated 11th April, 2018. Measure your website readability!
Page 1 of 2. https://readable.com
Nodejs Cheat Sheet Cheat Sheet
by digotetso via cheatography.com/58474/cs/15450/

NPM

npm init Create packag​e.json file


alt: npm init --yes

npm i Install npm package


alt: npm i --save-de
v
npm i --save

npm list lookup depend​encies &


version
alt: npm list --dept
h=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

By digotetso Not published yet. Sponsored by Readable.com


cheatography.com/digotetso/ Last updated 11th April, 2018. Measure your website readability!
Page 2 of 2. https://readable.com

You might also like