REST API - Setting up a Node.js development environment with Express.js (using Mongoose ORM / MongoDB Atlas): Libray management
March 2020
🔨 Library management using Node.js and Express.js. MDN tutorial 'Setting up a Node development environment'.
See the demo on Heroku.
Library management with CRUD. You can add books, genres, authors and book instances.
Read:
Create:
Delete / Update:
- Clone the local-version branch
- To run:
DEBUG=express-locallibrary-tutorial:* npm run devstart
- Open the app using http://localhost:3000/
See the demo on Heroku.
- Routes and controllers (MVC)
- Views with pug templates
- Forms (CRUD) with validation & sanitization
- Using a Database (with Mongoose and MongoDB Atlas)
- Deployment on Heroku
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
npm install -g express
npm install express-generator -g
express myApp --view=pug
cd myApp
npm install
On Windows, use this command:
SET DEBUG=express-locallibrary-tutorial:* & npm start
On macOS or Linux, use this command:
DEBUG=express-locallibrary-tutorial:* npm start
Enable server restart on file changes
npm install -g nodemon
npm install --save-dev nodemon
Add in package.json:
"scripts": {
"start": "node ./bin/www",
"devstart": "nodemon ./bin/www",
"serverstart": "DEBUG=express-locallibrary-tutorial:* npm run devstart"
},
On Windows, use this command:
SET DEBUG=express-locallibrary-tutorial:* & npm run devstart
On macOS or Linux, use this command:
DEBUG=express-locallibrary-tutorial:* npm run devstart
Installing Mongoose adds all its dependencies, including the MongoDB database driver.
npm install mongoose --save
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
npm install async --save
- async.parallel() to execute any operations that must be performed in parallel.
- async.series() for when we need to ensure that asynchronous operations are performed in series.
- async.waterfall() for operations that must be run in series, with each operation depending on the results of preceding operations.
Lightweight JavaScript date library for parsing, validating, manipulating, and formatting dates
npm install moment --save
node populatedb '<your atlas mongodb url>'
(don't forget quotes)
(I.E.: 'mongodb+srv://:@cluster0-mbdj7.mongodb.net/?retryWrites=true')
Performs both validation and sanitization of our form data.
npm install express-validator --save
- Validation checks that entered values are appropriate for each field (are in the right range, format, etc.) and that values have been supplied for all required fields.
- Sanitization removes/replaces characters in the data that might potentially be used to send malicious content to the server.
Compression middleware enables GZIP, which supports different compression schemes.
npm install compression --save
Helmet is a middleware package. It can set appropriate HTTP headers that help protect your app from well-known web vulnerabilities.
npm install helmet --save