Node JS Cheat Sheet + PDF Zero To Mastery
Node JS Cheat Sheet + PDF Zero To Mastery
Sheet
We created this Node JS Cheat Sheet initially for
students of our Node.js Bootcamp: Complete Node.js
Developer: Zero to Mastery. But we're now sharing it
with any and all Developers that want to learn and
remember some of the key functions and concepts of
Node JS, and have a quick reference guide to the
basics of Node JS.
Unsubscribe anytime.
Contents
Running Node.js
Running Node.js
Built-in Modules
Creating Modules
ECMAScript Modules
Node.js Packages
NPM Commands
package.json
node_modules
package-lock.json
Backend Concepts
Backend Concepts
Express.js
GET Routes
POST Routes
Routers
PM2 Commands
PM2 Commands
Useful Links
Useful Links
Running Node.js
For running Node.js:
Command Comments
setTimeout(() => {
console.log('hello');
}, 5000);
const fs = require('fs');
fs.readFileSync('hello.txt');
// OR...
Built-in Modules
Some modules like fs are built in to Node. These
modules contain Node-speci�c features.
// In src/fileModule.js
function read(filename) { }
function write(filename, data) { }
module.exports = {
read,
write,
};
// In src/sayHello.js
const { write } = require('./fileModule.js'
write('hello.txt', 'Hello world!');
// In src/fileModule.js
exports.read = function read(filename) { }
exports.write = function write(filename, data
ECMAScript Modules
The imports above use a syntax known as CommonJS
(CJS) modules. Node treats JavaScript code as
CommonJS modules by default. More recently, you
may have seen the ECMAScript module (ESM) syntax.
This is the syntax that is used by TypeScript.
// In src/fileModule.mjs
function read(filename) { }
function write(filename, data) { }
export {
read,
write,
};
// In src/sayHello.mjs
import { write } from './response.mjs';
write('hello.txt', 'Hello world!');
Node.js Packages
NPM Commands
Command Comments
npm install -g
Install a package globally.
<package>
package.json
Most Node applications we create include a
package.json �le, which means our Node applications
are also Node packages.
node_modules
This folder lives next to your package.json �le.
package-lock.json
The package-lock.json �le is automatically created by
NPM to track the exact versions of packages that are
installed in your node_modules folder. Share your
package-lock.json with other developers on your
team to ensure that everyone is running the exact
same versions of every package in the dependency
tree.
celebrity.on('success', () => {
console.log('Congratulations! You are the best!'
});
Backend Concepts
Client-server architecture
API
CRUD
RESTful
CRUD HTTP
Example
Operation method
PUT (or
PUT /cards/:cardId
more
Update (update an individual
rarely
card)
PATCH)
DELETE /cards/:cardId
(delete an individual
card) or more rarely
Delete DELETE
DELETE /cards (delete
the entire collection of
cards)
Express.js
GET Routes
POST Routes
Routers
// In src/cards.router.js
const cardsRouter = express.Router
// In src/api.js
const cardsRouter = require('./cards.router'
api.use('/cards', cardsRouter)
node-project/
node_modules/
data/
database.json
src/
models/
comment.model.js
post.model.js
routes/
routes/
feeds/
feed.router.js
feed.controller.js
posts/
post.router.js
post.controller.js
api.js
services/
mongo.js
app.js
server.js
.gitignore
package-lock.json
package.json
PM2 Commands
Command Comments