Learning to create a simple RESTful API for Authentication using Node.js + Express + MongoDB
This is a basic API skeleton written in JavaScript ES2015. Very useful to building a RESTful web APIs for our front-end platforms like Angular, Reactjs, etc.
This project will run on NodeJs using MongoDB as database. The code structure easy as any beginner can also adopt the flow and start building an API.
- Node.js 8+
- MongoDB 3.6+ (Recommended 4+)
- Clone the project from github. Change "myproject" to your project name.
git clone https://github.com/palashmon/mevn-auth-api.git ./myproject
cd myproject
npm install
- You will find a file named
.env.example
on root directory of project. - Create a new file by copying and pasting the file and then renaming it to just
.env
cp .env.example .env
- The file
.env
is already ignored, so you never commit your credentials.
npm run dev
If you need to add more models to the project just create a new file in /models/
and use them in the controllers.
If you need to add more routes to the project just create a new file in /routes/
and add it in /routes/api.js
it will be loaded dynamically.
If you need to add more controllers to the project just create a new file in /controllers/
and use them in the routes.
api/users/register
- Register the Userapi/users/login
- Signing in the Userapi/users/profile
- Return the User's dataapi/users/logout
- Remove the req.user property and clear the login session (if any)