- initialize a package
npm init -y
- install dependencies
a. axios for API:
npm install axios
b. Express.js: Install the Express framework, which simplifies API developmentnpm install express
c. mongoose: database storage d. dotenv: to store sensitive information e. nodemon: it restarts the server whenever a file is saved. - To run the app, use command:
nodemon app.js
ornpm start
if starter script is available in package.json file
- Login to your MongoDB account and create a new cluster.
- Connect your MongoDB database by selecting the
Node.js
driver. Add connection string to your main js file. - Create Model Schema
- create 'models' folder and add a js file inside the folder. A naming convention practice is to save the file according to the type of data you are using (eg. productModel.js)
- Create CRUD routes for endpoints in your main js file
- Add a middleware to parse JSON data
- Test API endpoints in Postman using GET, POST, UPDATE, and DELETE.
- Check if data is reflected in your MongoDB account.
- Alternative way of importing large data is to use MongoDB Compass. Click to download
- Locate Mongoose API endpoint where you will find it in your Postman
- Create a script.js file and use a Promise based HTTP client to fetch your data from the API. We'll be using Axios for this project.
- Use DOM manipulation to show the data in the front-end.
- Useful Javascript array methods are filter() to create a new array based on multiple arrays of document, map() to create a new set of array that calls out a function that provides each element in an array.
- Message: MongooseError: The
uri
parameter toopenUri()
must be a string, got "undefined". Make sure the first parameter tomongoose.connect()
ormongoose.createConnection()
is a string. Solution: Check mongoose connection. Check dotenv import is correct. - Message: fatal: the remote end hung up unexpectedly
Solution:
git config http.postBuffer 524288000
- Message: localhost:10000/getAllSubmitted:1Failed to load resource: net::ERR_BLOCKED_BY_CLIENT
https://blog.postman.com/how-to-create-a-rest-api-with-node-js-and-express/ https://www.youtube.com/watch?v=tXS_1VMhdT4 https://www.freecodecamp.org/news/build-a-restful-api-using-node-express-and-mongodb/ https://dev.to/kodebae/how-to-remove-a-leaked-env-file-from-github-permanently-3lei dummyjson Build Restful CRUD API with Node.js, Express and MongoDB by Devtamin Store html form data in MongoDb using Nodejs and express