Introduction:
Express.js is a popular framework for building web applications in Node.js. It provides a minimalistic and flexible approach to developing server-side applications.
However, setting up an Express.js project with MongoDB integration can be a tedious and time-consuming task. That's where Create Mongo Express comes to the rescue! In this article, we'll explore how Create Mongo Express can simplify your Express.js development workflow and boost your productivity.
Create Mongo Express is a simple and powerful CLI tool that automates the setup of an Express.js application with MongoDB connection. With this tool, you can quickly generate a fully configured Express.js project, complete with file
based preconfigured routes and seamless integration with MongoDB.
Installation
To install the package, make sure you have Node.js and npm installed on your machine. Then, run the following command:
npx create-mongo-express project-name
Next, you will be asked to provide the MongoDB URI.
Enter the MongoDB URI:
Ensure that your MongoDB URI is in the following format:
mongodb+srv://mongoDbUser:mongoDbUserPass@cluster0.s938843o0.mongodb.net/databaseName?retryWrites=true&w=majority
Running the Server
After the setup is complete, navigate to the project folder:
cd project-name
Next, start the server by running the following command:
npm run dev
Your server will run on port 3500 by default.
If you wish to change the port, you can modify the server.js
file located in the root folder.
Creating a New Route
To create a new route in the api folder, follow these steps:
Inside the project folder, locate the api folder. This is where you can add your custom routes.
Create a new file with a descriptive name for your route. For example, if you want to create a route for managing products, you can create a file named products.js
inside the api folder.
Open the newly created file and add the necessary code to define your route. Here's an example:
const express = require('express');
const router = express.Router();
const Customer = require('../model/Customer');
// GET /api/customers
router.get('/', (req, res) => {
res.send('This is the /customers route');
});
// GET /api/customers/:id
router.get('/:id', (req, res) => {
console.log(req.params.id);
res.send(`This is the /customers/${req.params.id} route`);
});
// POST /api/customers
router.post('/', async (req, res) => {
const { first_name, last_name, email, phone } = req.body;
if (!email || !first_name || !last_name || !phone) {
return res
.status(400)
.json({ message: 'Please fill out all the required fields' });
}
try {
const duplicateCustomer = await Customer.findOne({ email }).exec();
if (duplicateCustomer) {
return res.status(409).json({
message: `Another user with email ${email} already exists, login instead`,
});
}
// Create and store new user
const result = await Customer.create({
first_name,
last_name,
email,
phone,
});
return res.status(200).json({
message: 'User created successfully',
first_name: result.first_name,
last_name: result.last_name,
email: result.email,
phone: result.phone,
});
} catch (error) {
res.status(500).json({ message: error.message });
}
});
module.exports = router;
Route exports
Make sure all your routes are exported properly like below, otherwise your it would show a 404 page.
const express = require('express');
const router = express.Router();
// GET /api/products
router.get('/', (req, res) => {
res.send('This is the /products route');
});
module.exports = router;
Customize the route logic according to your requirements. You can retrieve data from the database, create new records, update existing records, or delete records based on the HTTP methods and route paths you define.
Save the file.
Your new route is now available in the Express server. You can access it using the route path you defined. For example, if you created a products.js
route as shown above, you can access the route endpoints at /api/products
, /api/products/:id
, etc.
That's it! You have successfully created a new route in the api folder. Feel free to add more routes and customize them as per your application's needs.
Mongoose models
You can add your own mongoose models in the model folder at the root level. Here's an example:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const customerSchema = new Schema({
first_name: String,
last_name: String,
email: String,
phone: String,
});
module.exports = mongoose.model('Customer', customerSchema);
Change Route Structure
If you want to change your routes to something else instead of the default /api
you can change this in the server.js
file at the root level.
// File-based routing
function registerRoutes(file) {
const routeName = path.parse(file).name;
const routePath = path.resolve(apiFolderPath, file);
const routeModule = require(routePath);
if (typeof routeModule === 'function') {
const route = `/api/${routeName}`;
app.use(route, routeModule);
console.log(`Route created: ${route}`);
} else {
console.error(`Invalid route file: ${file}. Skipping...`);
}
}
Top comments (0)