Mongoose Express Generator
This VS Code extension generates boilerplate code for Mongoose/Express.js applications. It helps you quickly create models, controllers, routes, and app.js files with a standardized structure.
Features
- Generate Mongoose models with customizable fields
- Generate Express controllers with CRUD operations
- Generate Express routes with proper endpoints
- Generate app.js configuration
Commands
The extension provides the following commands, which can be run from the command palette (Ctrl+Shift+P):
- Generate Mongoose Model: Creates a new Mongoose model file
- Generate Express Controller: Creates a new Express controller with CRUD operations
- Generate Express Route: Creates a new Express router with proper endpoints
- Generate Express App.js: Creates an app.js file with basic Express and MongoDB setup
Usage
- Open the command palette (Ctrl+Shift+P)
- Type one of the commands (e.g., "Generate Mongoose Model")
- Follow the prompts to enter the required information
- The extension will generate the file and open it in the editor
Example: Generate a Model
When generating a model, you'll need to:
- Enter the model name (e.g., "Product")
- Enter fields in the format
name:type,required
(e.g., "name:String,required price:Number desc:String")
Example: Generate a Controller or Route
When generating a controller or route, you'll need to:
- Enter the model name (e.g., "Product")
The extension will automatically create the appropriate controller or route file with CRUD operations.
Installation
- Download the VSIX file
- Open VS Code
- Go to Extensions view
- Click on "..." in the top right corner
- Select "Install from VSIX..."
- Select the downloaded VSIX file
Generated Files Structure
Model
const mongoose = require('mongoose');
const ProductSchema = mongoose.Schema(
{
name: {type: String, required: true },
price: {type: String, required: true},
desc: {type: String, required: true}
},
{
timestamps: true,
}
);
module.exports = mongoose.model('Product', ProductSchema);
Controller
const Product = require('../models/productModel');
exports.getProducts = async (req, res) => {
try {
const products = await Product.find();
res.status(201).json(products)
} catch (error) {
res.status(500).json("Server Error")
}
};
exports.createProduct = async (req, res) => {
try {
const newProduct = await Product.create(req.body);
res.status(201).send({
message: "Product Created",
product: newProduct
});
} catch (error) {
res.status(500).send("Server Error");
}
};
// ... update and delete methods ...
Route
const express = require('express');
const router = express.Router();
const {
getProducts,
createProduct,
updateProduct,
deleteProduct,
} = require('../controllers/productController');
router.route('/products')
.get(getProducts)
.post(createProduct);
router.route('/products/:id')
.put(updateProduct)
.delete(deleteProduct);
module.exports = router;
App.js
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
app.use(express.json());
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log("MongoDB connected"))
.catch((err) => console.error("MongoDB error:", err));
// Import routes
// app.use('/api', require('./routes/yourRoutes'));
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`http://localhost:${port}`);
});