A full CRUD backend for managing farmer records built with Node.js, Express, TypeScript and MongoDB. Uses a clean OOP structure with controllers, services and repositories.
- CRUD: Create, get single, list, update, delete farmers
- Search: Search by name, village or crop
- Filter: Filter by village and crop
- Sorting: Sort by any field (prefix with
-for descending) - Pagination: Page and limit query params
- Validation: Request validation with express-validator
- Error handling: Centralised error middleware with clear messages
- Authentication: JWT-based auth; register and login to get a token; farmer routes are protected
controllers– handle HTTP, delegate to servicesservices– business logic, call repositoriesrepositories– data access (Mongoose)models– Mongoose schemasmiddleware– auth, validation, error handlingvalidators– express-validator rulesutils– interfaces, custom errors
- Clone the repo and install dependencies:
npm install- Copy
.env.exampleto.envand set:
PORT– server port (default 8080)MONGODB_URI– MongoDB connection stringJWT_SECRET– secret for signing JWTs
- Run the app:
npm run devPOST /auth/register– body:{ "email": "...", "password": "..." }– returns{ "token": "..." }POST /auth/login– body:{ "email": "...", "password": "..." }– returns{ "token": "..." }
POST /farmers– create farmer – body:{ "name", "village", "crop", "landArea", "phone" }GET /farmers– list with query:?search=&village=&crop=&sort=-createdAt&page=1&limit=10GET /farmers/:id– get one farmerPATCH /farmers/:id– update farmer (partial body)DELETE /farmers/:id– delete farmer
Node.js, Express, TypeScript, Mongoose, bcryptjs, jsonwebtoken, express-validator.