A Node.js + Express + MongoDB REST API for managing contacts with JWT authentication.
This project allows users to register, login, and securely perform CRUD operations on their contacts.
- Node.js
- Express.js
- MongoDB
- JWT
- bcryptjs
- User registration & login with hashed passwords (
bcryptjs) - JWT-based authentication (private routes protected with middleware)
- CRUD operations on contacts (Create, Read, Update, Delete)
- Centralized error handling
- MongoDB integration with Mongoose
- Node.js
- Express.js
- MongoDB + Mongoose
- JWT (jsonwebtoken)
- bcryptjs
📂 Contacts-Manager-API
├── 📂 config/ # Database connection settings
├── 📂 controllers/ # User & Contact controllers
├── 📂 middleware/ # Error handler & JWT validation
├── 📂 models/ # Mongoose models (User, Contact)
├── 📂 routes/ # API route definitions
├── .gitignore # Ignored files & folders
├── LICENSE # Project license (MIT)
├── README.md # Project documentation
├── constants.js # Constants (error codes/messages)
├── package-lock.json # Dependency lock file
├── package.json # Project metadata & dependencies
├── server.js # Main entry point of the app
git clone https://github.com/Arsh-pixel-cmd/Contacts-Manager-API
cd Contacts-Manager-APInpm installCreate a .env file in the root:
PORT=4000
CONNECTION_STRING=your_mongoDb_URI
ACCESS_TOKEN_SECRET=my_super_secret_key
npm start✅ Expected console output
server is listening on port 4000
Connection Established : Host = localhost and Name = test
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/users/register |
Register a new user |
| POST | /api/users/login |
Login and receive JWT |
| GET | /api/users/current |
Get logged-in user info (requires token) |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/contacts |
Get all contacts for the logged-in user |
| POST | /api/contacts |
Create a new contact |
| GET | /api/contacts/:id |
Get a contact by ID |
| PUT | /api/contacts/:id |
Update a contact by ID |
| DELETE | /api/contacts/:id |
Delete a contact by ID |
POST your_local_server_url/api/users/register
{
"userName": "Arsh",
"userEmail": "arsh@example.com",
"password": "123456"
}POST your_local_server_url/api/users/login
{
"userEmail": "arsh@example.com",
"password": "123456"
}✅ Response includes a JWT token.
Add this header in Postman:
Authorization: Bearer <your-token>POST your_local_server_url/api/contacts
{
"name": "Jane Doe",
"email": "jane@example.com",
"phone": "9876543210"
}- You must register or login first to get a JWT token.
- All contact routes require a valid JWT in the
Authorizationheader. - Current login token expiry is set to 1 minute — consider updating it to
30mor1hinuserControllers.js. - Default MongoDB database is test unless you change it in
CONNECTION_STRING.
Contributions are welcome!
- Fork the repo
- Create a new branch (
feature/your-feature) - Commit your changes
- Push to the branch
- Open a Pull Request
📜 License
This project is licensed under the MIT License.