A complete Node.js backend with MongoDB, JWT authentication, and user profile management.
- User Registration
- User Login with JWT
- Protected Routes
- User Profile Create/Update
- Error Handling Middleware
- MongoDB Integration
- Password Hashing with bcrypt
npm installCopy .env.example to .env and update the values:
PORT=5000
MONGO_URI=mongodb://localhost:27017/mybackend
JWT_SECRET=your_secret_key
NODE_ENV=development
Development mode with auto-restart:
npm run devProduction mode:
npm startRegister User
POST /api/users/register
Body: { "name": "John Doe", "email": "john@example.com", "password": "123456" }
Login User
POST /api/users/login
Body: { "email": "john@example.com", "password": "123456" }
Get User Profile
GET /api/users/profile
Headers: { "Authorization": "Bearer <token>" }
Update User Profile
PUT /api/users/profile
Headers: { "Authorization": "Bearer <token>" }
Body: {
"name": "John Updated",
"profile": {
"bio": "Software Developer",
"avatar": "https://example.com/avatar.jpg",
"phone": "+1234567890"
}
}
my-backend/
├─ package.json
├─ .env
├─ server.js
└─ src/
├─ config/
│ └─ db.js
├─ models/
│ └─ User.js
├─ controllers/
│ └─ userController.js
├─ routes/
│ └─ userRoutes.js
├─ middleware/
│ ├─ authMiddleware.js
│ └─ errorMiddleware.js
└─ utils/
└─ generateToken.js
Register:
curl -X POST http://localhost:5001/api/users/register \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","password":"123456"}'Login:
curl -X POST http://localhost:5001/api/users/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"123456"}'Get Profile:
curl -X GET http://localhost:5001/api/users/profile \
-H "Authorization: Bearer YOUR_TOKEN_HERE"- Node.js v14+
- MongoDB running locally or remote connection