Node Cheatsheet PDF
Node Cheatsheet PDF
npm Commands
Command Description
npm init Initializes a node project. Creates packages.json to track required
modules/packages, as well as the node_modules folder to track
imported packages.
npm install Installs any requirements for the local node package based on the
contents of package.json.
npm install <package-name> Installs a package from NPM’s own repository as well as any
requirements specified in that package’s package.json file.
Web Service A type of API that supports HTTP Requests, returning data such as
JSON or plain text.
Express A module for simplifying the http-server core module in Node to
implement APIs
API Documentation A file detailing the endpoints, usage, and functionality of various
endpoints of an API.
fs The “file system” module with various functions to process data in the file system.
glob Allows for quick traversal and filtering of files in a complex directory.
multer Used to support FormData POST requests on the server-side so we can access the req.body parameters.
promise-mysql Promisified wrapper over mysql module - each function in the mysql module returns a promise instead of
taking a callback as the last argument (recommended).
res.send(data); Sends the data back to the client, signaling an end to the
response (does not terminate your JS program).
res.send(“Hello”);
res.send({ “msg” : “Hello” });
fs.writeFile(filename, data, “utf8”, callback); Writes datastring to the file specified by filename ,
overwriting its contents if it already exists. If an error
occurs, erroris passed to the callback function.
fs.writeFile(“file.txt”, “new contents”, “utf8”,
(err) => { ... }
);
fs.appendFile(filename, data, “utf8”, callback); Writes datato the file specified by filename ,
appending to its contents. Creates a new file if the
filename does not exist. If an error occurs, erroris
fs.appendFile(“file.txt”, “added contents”, “utf8”, passed to the callback function.
(err) => { ... }
);
path.extname(pathStr); Returns the file type/file extension of the pathStr. Ex. “.png” for “img/picture.png”
path.dirname(pathStr); Returns the directory name of the pathStr. Ex: “img/” for “img/picture.png”
db.query(qryString); Executes the SQL query. If the query is a SELECT statement, returns a
Promise that resolves to an array of RowDataPackets with the records
matching the qryString passed. If the query is an INSERT statement,
the Promise resolves to an OkPacket. Throws an error if something
goes wrong during the query.
db.query(qryString, [placeholders]); When using variables in your query string, you should use ?
placeholders in the string and populate [placeholders] with the
variable names to sanitize the input against SQL injection