@misterzik/espressojs
Version:
EspressoJS Introducing Espresso.JS, your ultimate Express configuration starting point and boilerplate. With its simplicity and lack of opinionation, EspressoJS offers plug-and-play configurations built on top of Express.
150 lines (126 loc) • 3.17 kB
JavaScript
/*
* EspressoJS - MongoDB Integration Example
* This file demonstrates how to use MongoDB with EspressoJS
*/
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const { asyncHandler, AppError } = require('../server/middleware/errorHandler');
// Example Schema
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Name is required'],
trim: true,
maxlength: [50, 'Name cannot exceed 50 characters']
},
email: {
type: String,
required: [true, 'Email is required'],
unique: true,
lowercase: true,
match: [/^\S+@\S+\.\S+$/, 'Please provide a valid email']
},
age: {
type: Number,
min: [0, 'Age must be positive']
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
});
// Update timestamp on save
UserSchema.pre('save', function(next) {
this.updatedAt = Date.now();
next();
});
const User = mongoose.model('User', UserSchema);
// GET all users with pagination
router.get('/users', asyncHandler(async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const skip = (page - 1) * limit;
const users = await User.find()
.select('-__v')
.limit(limit)
.skip(skip)
.sort({ createdAt: -1 });
const total = await User.countDocuments();
res.status(200).json({
status: 'success',
results: users.length,
pagination: {
page,
limit,
total,
pages: Math.ceil(total / limit)
},
data: { users }
});
}));
// GET user by ID
router.get('/users/:id', asyncHandler(async (req, res) => {
const user = await User.findById(req.params.id).select('-__v');
if (!user) {
throw new AppError('User not found', 404);
}
res.status(200).json({
status: 'success',
data: { user }
});
}));
// POST create new user
router.post('/users', asyncHandler(async (req, res) => {
const user = await User.create(req.body);
res.status(201).json({
status: 'success',
data: { user }
});
}));
// PUT update user
router.put('/users/:id', asyncHandler(async (req, res) => {
const user = await User.findByIdAndUpdate(
req.params.id,
req.body,
{
new: true,
runValidators: true
}
).select('-__v');
if (!user) {
throw new AppError('User not found', 404);
}
res.status(200).json({
status: 'success',
data: { user }
});
}));
// DELETE user
router.delete('/users/:id', asyncHandler(async (req, res) => {
const user = await User.findByIdAndDelete(req.params.id);
if (!user) {
throw new AppError('User not found', 404);
}
res.status(204).send();
}));
// GET users by query
router.get('/users/search/:query', asyncHandler(async (req, res) => {
const { query } = req.params;
const users = await User.find({
$or: [
{ name: { $regex: query, $options: 'i' } },
{ email: { $regex: query, $options: 'i' } }
]
}).select('-__v');
res.status(200).json({
status: 'success',
results: users.length,
data: { users }
});
}));
module.exports = router;