Login form using Node.js and MongoDB
Follow these simple steps to learn how to create a login form using Node.js and MongoDB. Login form allows users to login to the website after they have created their account using the signup form.
Installation of modules:
$ npm install ejs
$ npm install express --save
Express is a module framework for Node that you can use for applications.
$ npm install mongoose
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
$ npm install body-parser --save
Body-parser allows express to read the body and then parse that into a JSON object that we can understand.
npm install express-session --save
Express.js uses a cookie to store a session id.
npm install passport passport-local --save npm install passport-local-mongoose --save
- Passport is authentication middleware for Node. js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application. A comprehensive set of strategies support authentication using a username and password, Facebook, Twitter, and more.
Steps to run the program:
- The project structure will look like this:
- And the content of views directory will look like this:
- Rest files will be the created after you run npm init for initialising .json file.
- Run index.js file using below command:
- OR If you don’t have nodemon installed you can run:
- Now go to your browser and type the following URL:
- First you will need to signup then only you can login into the app.
- Now you have successfully signup, Now this is login form as shown below:
- This login form will take you to the Secret page as shown below:
So this is how you can create login form using Node.js and MongoDB.