Challenge:
Build a Node Express app with MongoDB to store and serve content and get data from mongodb.
- Plain JavaScript
- Node.js
- Express (JS framework)
- MongoDB (Database)
- Mongoose (JS framework)
- Cloud9 (Online IDE)
- Postman (testing APIs)
- 1- Run npm init and create the jSON file
- 2- Installed the npm packages.
- 3- required dependencies and node modules --save
- 4- Create the Restful routing this application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST ,PUT , PATCH, Delete …HTTP verbs...)
- 5- Create the EJS templates and generate html using JavaScript
- 6- Write the routes and test them with postman
- 7- Use forms to send Post requests
- 8- Add in body-parser
- 9- Add a navbar to all templates
- 10- Set up mongoose schemas and models Mongoose to provide database access. object schema Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.” asynchronous operation means that a process operates independently of other processes, Mongoose acts as a front end to MongoDB, which is an open source NoSQL database that uses a document-oriented data model. A “collection” of “documents”, document storage and query system looks very much like JSON, and is hence familiar to JavaScript developers.Mongoose”
- 11- Create a models directory
- 12- Use module.exports
- 13- Require everything correctly!
- 14- Add a seeds.js file
- 16- Add the comment new and create routes
- 17- Add the new comment form
- 18- Add public directory for styling
- 19- Add User Model
- 20- Install all packages needed for auth
- 21- Define User model
- 22- Register
- 23- Configure Passport middlware “can authenticate against a local/remote database instance or use the single sign-on using OAuth providers for Facebook, Twitter, Google, etc. to authenticate with your social media accounts, or you can choose from an extensive list of providers which support authentication with Passport and provide a node module for that. n this tutorial, we will use the Local Authentication Strategy of Passport and authenticate the users against a locally configured Mongo DB instance, storing the user details in the database. For using the Local Authentication Strategy, we need to install the passport-local module: npm install passport-local
- 24- Serializing and Deserializing User Instances
- 25- Passport also needs to serialize and deserialize user instance from a session store in order to support login sessions, so that every subsequent request will not contain the user credentials. It provides two methods serializeUser and deserializeUser
- 26- Add register routes
- 27- Add register template
- 28- Add login routes
- 29- Add login template
- 30- Add logout route
- 31- Prevent user from adding a comment if not signed in
- 32- Show/hide auth links in navbar
- 33- Use Express router to reorganize all routes
- 34- Associate users and comments
- 35- Save author's name to a comment automatically
- 36- Prevent an unauthenticated user from creating a new instance
- 37- Save username+id to newly created instance
- 38- Add Method-Override
- 40- Add Destroy Route
- 41- Add Delete button
- 42- Authorization
- 43- User can only edit his/her instances
- 44- User can only delete his/her instances
- 45- Hide/Show edit and delete buttons
- 46- Add Edit route for comments
- 47- Add Edit button
- 48- Add Update route
- 49- Add Destroy route
- 50- Add Delete button
- 51- Install and configure connect-flash
- Deploy on Heroku
- Connect to Mongolab
In this exercise i used:
I use built this app to train myself on the backend I just styled the templates very little because my main focus was to learn how to properly connect and creates the logic to back-end and the components and features accessed by a user through a front-end.
This is a NODEjs/ express app with CRUD functionality uses a callback functions whose parameters are request and response objects. Stores and retrieves JSON data using MongoDB, a NOSQL database hook up in a sandbox database on mLab, free cloud-hosted "sandbox" database. Data sent through HTTP requests
Result:
Check the result
See the source code