node-express-mongoose
A boilerplate application for building web apps using node and mongodb
Top Related Projects
Fast, unopinionated, minimalist web framework for node.
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript π
Expressive middleware for node.js using ES2017 async functions
Fast and low overhead web framework, for Node.js
The API and real-time application framework
π Strapi is the leading open-source headless CMS. Itβs 100% JavaScript/TypeScript, fully customizable, and developer-first.
Quick Overview
Node-express-mongoose is a boilerplate application for building web applications using Node.js, Express, and MongoDB with Mongoose. It provides a structured starting point for developers, including user authentication, MVC architecture, and various helpful utilities.
Pros
- Comprehensive setup with essential features like authentication and MVC structure
- Well-organized codebase following best practices
- Includes testing setup with Mocha and Chai
- Provides useful utilities and middleware out of the box
Cons
- May be overwhelming for beginners due to its comprehensive nature
- Some dependencies might be outdated, requiring manual updates
- Opinionated structure may not fit all project requirements
- Limited customization options without significant modifications
Code Examples
- Setting up a new route:
// app/routes/home.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.render('home/index', {
title: 'Home'
});
});
module.exports = router;
- Creating a new model:
// app/models/article.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ArticleSchema = new Schema({
title: { type: String, default: '', trim: true },
body: { type: String, default: '', trim: true },
user: { type: Schema.ObjectId, ref: 'User' },
createdAt: { type: Date, default: Date.now }
});
mongoose.model('Article', ArticleSchema);
- Using middleware for authentication:
// config/middlewares/authorization.js
exports.requiresLogin = function (req, res, next) {
if (req.isAuthenticated()) return next();
if (req.method == 'GET') req.session.returnTo = req.originalUrl;
res.redirect('/login');
};
Getting Started
-
Clone the repository:
git clone https://github.com/madhums/node-express-mongoose.git cd node-express-mongoose
-
Install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env
-
Start the application:
npm start
The application will be running at http://localhost:3000
.
Competitor Comparisons
Fast, unopinionated, minimalist web framework for node.
Pros of Express
- Widely adopted and maintained web application framework for Node.js
- Extensive ecosystem with numerous middleware and plugins
- Highly flexible and minimalist, allowing developers to structure applications as needed
Cons of Express
- Requires more setup and configuration for complex applications
- Less opinionated, which may lead to inconsistent project structures across teams
- Doesn't include built-in ORM or database integration
Code Comparison
Express:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
node-express-mongoose:
const express = require('express');
const mongoose = require('mongoose');
const app = express();
mongoose.connect('mongodb://localhost/myapp');
app.get('/', (req, res) => {
res.send('Hello World!');
});
Key Differences
- node-express-mongoose provides a more opinionated structure for Express applications
- It includes MongoDB integration with Mongoose out of the box
- node-express-mongoose offers pre-configured middleware and settings for common use cases
- Express is more lightweight and flexible, allowing developers to choose their own database and ORM
- node-express-mongoose is better suited for rapid prototyping of MongoDB-based applications
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript π
Pros of Nest
- Built-in dependency injection and modular architecture for better scalability
- TypeScript-first approach with strong typing and decorators
- Comprehensive documentation and active community support
Cons of Nest
- Steeper learning curve due to its opinionated structure
- Potentially overkill for small, simple projects
- Higher initial setup time compared to Express-based solutions
Code Comparison
node-express-mongoose:
const express = require('express');
const mongoose = require('mongoose');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
Nest:
import { Controller, Get } from '@nestjs/common';
@Controller()
export class AppController {
@Get()
getHello(): string {
return 'Hello World!';
}
}
Summary
node-express-mongoose provides a straightforward, lightweight boilerplate for Node.js applications using Express and Mongoose. It's ideal for quick prototypes and smaller projects.
Nest, on the other hand, offers a more structured, scalable framework with built-in architectural patterns. It's better suited for larger, enterprise-grade applications but requires more initial setup and learning.
The code comparison illustrates the difference in approach: node-express-mongoose uses a more traditional Express setup, while Nest employs decorators and a class-based structure for defining routes and controllers.
Expressive middleware for node.js using ES2017 async functions
Pros of Koa
- Lightweight and minimalist design, offering better performance and flexibility
- Built-in support for async/await, simplifying asynchronous code handling
- Modular architecture allowing developers to add only needed functionality
Cons of Koa
- Smaller ecosystem and community compared to Express-based solutions
- Steeper learning curve for developers familiar with Express middleware
- Requires additional modules for common functionalities (e.g., routing)
Code Comparison
node-express-mongoose:
app.get('/', function(req, res) {
res.send('Hello World');
});
Koa:
app.use(async ctx => {
ctx.body = 'Hello World';
});
Key Differences
- node-express-mongoose is a boilerplate project combining Express, Mongoose, and other common modules
- Koa is a lightweight web framework focusing on core functionality
- node-express-mongoose provides a more opinionated structure, while Koa offers greater flexibility
- Koa uses a cascading middleware architecture, different from Express's linear middleware approach
Use Cases
- node-express-mongoose: Rapid development of full-stack Node.js applications with MongoDB integration
- Koa: Building lightweight, high-performance web applications and APIs with custom middleware stacks
Fast and low overhead web framework, for Node.js
Pros of Fastify
- Significantly faster performance due to its optimized architecture
- Built-in schema validation and serialization for improved data handling
- Extensible plugin system for easy feature additions
Cons of Fastify
- Steeper learning curve compared to Express-based frameworks
- Smaller ecosystem and community support
- Less mature, with potential for more frequent breaking changes
Code Comparison
node-express-mongoose:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
Fastify:
const fastify = require('fastify')();
fastify.get('/', async (request, reply) => {
return 'Hello World!';
});
Key Differences
- Fastify uses async/await by default, while Express relies on callbacks
- Fastify's route handlers return values instead of calling
res.send()
- node-express-mongoose includes Mongoose integration, while Fastify requires separate setup for database connections
Use Cases
- node-express-mongoose: Ideal for rapid prototyping and projects requiring a familiar, well-established stack
- Fastify: Better suited for high-performance applications and microservices where speed is crucial
Community and Ecosystem
- node-express-mongoose: Larger community, more third-party middleware, and extensive documentation
- Fastify: Growing community, focused on performance-oriented plugins, and comprehensive core documentation
The API and real-time application framework
Pros of Feathers
- More comprehensive framework with built-in real-time capabilities and service-oriented architecture
- Supports multiple databases and ORMs out of the box
- Extensive plugin ecosystem for easy extensibility
Cons of Feathers
- Steeper learning curve due to its more opinionated structure
- May be overkill for simpler applications
- Less flexibility in choosing specific components or libraries
Code Comparison
node-express-mongoose:
const express = require('express');
const mongoose = require('mongoose');
const app = express();
mongoose.connect('mongodb://localhost/myapp');
app.use(express.json());
Feathers:
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const mongoose = require('feathers-mongoose');
const app = express(feathers());
app.configure(express.rest());
app.configure(mongoose);
Summary
Feathers offers a more feature-rich and opinionated framework with real-time capabilities and service-oriented architecture, while node-express-mongoose provides a simpler, more flexible boilerplate for Express and Mongoose integration. Feathers has a steeper learning curve but offers more out-of-the-box functionality, whereas node-express-mongoose allows for more customization and is better suited for simpler applications.
π Strapi is the leading open-source headless CMS. Itβs 100% JavaScript/TypeScript, fully customizable, and developer-first.
Pros of Strapi
- Provides a full-featured, customizable admin panel out-of-the-box
- Offers a plugin system for easy extensibility and integration with third-party services
- Includes built-in user authentication and role-based access control
Cons of Strapi
- Steeper learning curve due to its more complex architecture and features
- May be overkill for simple projects that don't require a full CMS
- Less flexibility in database schema design compared to a bare-bones setup
Code Comparison
Strapi (Content-Type definition):
module.exports = {
attributes: {
title: {
type: 'string',
required: true
},
content: {
type: 'richtext'
}
}
};
Node-Express-Mongoose (Mongoose schema):
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
title: { type: String, required: true },
content: String
});
Both projects provide ways to define data models, but Strapi uses a more abstracted approach with its Content-Types, while Node-Express-Mongoose relies on Mongoose schemas directly. Strapi's approach offers more built-in features and integrations, while Node-Express-Mongoose provides more low-level control over the database schema and interactions.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Node Express Mongoose
A boilerplate application for building web apps using express, mongoose and passport.
Read the wiki to understand how the application is structured.
Usage
git clone https://github.com/madhums/node-express-mongoose.git
cd node-express-mongoose
npm install
cp .env.example .env
npm start
Checkout the apps that are built using this approach
Docker
You can also use docker for development. Make sure you run npm install on your host machine so that code linting and everything works fine.
npm i
cp .env.example .env
Start the services
docker-compose up -d
View the logs
docker-compose logs -f
In case you install a npm module while developing, it should also be installed within docker container, to do this first install the module you want with simple npm i module name
, then run it within docker container
docker-compose exec node npm i
If you make any changes to the file, nodemon should automatically pick up and restart within docker (you can see this in the logs)
To run tests
docker-compose exec -e MONGODB_URL=mongodb://mongo:27017/noobjs_test node npm test
Note that we are overriding the environment variable set in .env
file because we don't want our data erased by the tests.
Note: The difference between exec and run is that, exec executes the command within the running container and run will spin up a new container to run that command. So if you want to run only the tests without docker-compose up, you may do so by running docker-compose run -e MONGODB_URL=mongodb://mongo:27017/my_app_test node npm test
Deployment
If you want to deploy to heroku, you can follow this article
License
MIT
Top Related Projects
Fast, unopinionated, minimalist web framework for node.
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript π
Expressive middleware for node.js using ES2017 async functions
Fast and low overhead web framework, for Node.js
The API and real-time application framework
π Strapi is the leading open-source headless CMS. Itβs 100% JavaScript/TypeScript, fully customizable, and developer-first.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot