
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
vs-express-api-builder
Advanced tools
A powerful, simple, and effective Express.js API builder that helps you create RESTful APIs with minimal code
A powerful, simple, and effective Express.js API builder that helps you create RESTful APIs with minimal code.
Our package is different from others because it focuses on simplicity and effectiveness:
If you're new to building APIs, check out our Beginner's Guide for step-by-step instructions.
npm install vs-express-api-builder
import express from 'express';
import { Model, DatabaseConnection, APICreator } from 'vs-express-api-builder';
// Define your model inline
const UserModel = new Model({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
age: { type: Number, min: 0 }
});
// Create Express app
const app = express();
app.use(express.json());
// Connect to MongoDB
await DatabaseConnection.getInstance().connect({
url: 'mongodb://localhost:27017/myapp'
});
// Create API endpoints
const userAPI = new APICreator({
path: '/users',
model: UserModel
});
// Use the API routes
app.use('/api', userAPI.getRouter());
// Start the server
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
That's it! You now have a fully functional REST API with the following endpoints:
GET /api/users
- List all usersGET /api/users/:id
- Get a specific userPOST /api/users
- Create a new userPUT /api/users/:id
- Update a userDELETE /api/users/:id
- Delete a userDefine your models inline with validation rules:
const ProductModel = new Model({
name: { type: String, required: true },
price: { type: Number, required: true, min: 0 },
category: { type: String, enum: ['electronics', 'clothing', 'books'] },
inStock: { type: Boolean, default: true }
});
Define relationships between models:
const OrderModel = new Model({
user: { type: String, ref: 'User', required: true },
products: [{ type: String, ref: 'Product' }],
total: { type: Number, required: true }
});
Add validation schemas using Zod:
import { z } from 'zod';
const productAPI = new APICreator({
path: '/products',
model: ProductModel,
validation: {
create: z.object({
name: z.string().min(2),
price: z.number().min(0),
category: z.enum(['electronics', 'clothing', 'books']),
inStock: z.boolean().optional()
})
}
});
Build complex queries with a fluent interface:
// Find all products in the electronics category that are in stock
const products = await ProductModel
.where('category').equals('electronics')
.where('inStock').equals(true)
.sort('price', 'asc')
.limit(10)
.exec();
// Find orders with populated user and products
const orders = await OrderModel
.where('total').gt(100)
.populate('user')
.populate('products')
.exec();
Add hooks to extend functionality:
const userAPI = new APICreator({
path: '/users',
model: UserModel,
hooks: {
beforeCreate: async (req, res, next) => {
// Hash password before creating user
req.body.password = await hashPassword(req.body.password);
next();
},
afterCreate: async (req, res, next) => {
// Send welcome email after user is created
await sendWelcomeEmail(res.locals.user.email);
next();
}
}
});
Configure middleware with a single object:
const api = new APICreator({
path: '/api',
model: MyModel,
middleware: {
auth: true, // Enable authentication
rateLimit: true, // Enable rate limiting
cache: true, // Enable response caching
cors: true // Enable CORS
}
});
Check out our examples directory for more usage examples:
For more detailed documentation, visit our documentation site.
MIT
FAQs
A powerful, simple, and effective Express.js API builder that helps you create RESTful APIs with minimal code
The npm package vs-express-api-builder receives a total of 10 weekly downloads. As such, vs-express-api-builder popularity was classified as not popular.
We found that vs-express-api-builder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.