Expert guidance for building a Node.js/Express task manager API with JWT authentication, MongoDB/Mongoose, and production-ready best practices.
Expert guidance for building a scalable, secure task manager API using Node.js, Express, JWT authentication, and MongoDB/Mongoose.
This project implements a full-featured task management backend with:
1. **Structure**: Follow MVC pattern (models, controllers, routes, middleware)
2. **Async/Await**: Use async/await for all database operations; handle errors with try/catch
3. **Validation**: Validate all inputs using express-validator or Joi
4. **Error Handling**: Centralized error handler middleware; return consistent JSON error responses
5. **Environment Variables**: Store secrets (JWT_SECRET, MONGO_URI) in `.env` using dotenv
- `POST /auth/register` — user registration
- `POST /auth/login` — user login (returns JWT)
- `GET /users/me` — get current user profile (protected)
- `PUT /users/me` — update profile (protected)
- `GET /tasks` — list tasks (protected, filtered by user)
- `POST /tasks` — create task (protected)
- `GET /tasks/:id` — get single task (protected)
- `PUT /tasks/:id` — update task (protected)
- `DELETE /tasks/:id` — delete task (protected)
```js
const taskSchema = new mongoose.Schema({
title: { type: String, required: true },
description: { type: String },
status: { type: String, enum: ['todo', 'in-progress', 'done'], default: 'todo' },
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
dueDate: { type: Date }
}, { timestamps: true });
```
```js
const jwt = require('jsonwebtoken');
module.exports = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Access denied' });
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch (err) {
res.status(401).json({ error: 'Invalid token' });
}
};
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/nodejs-task-manager-backend-with-jwt-auth/raw