jfullstackdev/codespaces-template-mern
a Codespaces Template for MERN with MongoDB, Express, React (via Vite), and Node.js
Codespaces Template for MERN
a Codespaces Template for MERN with MongoDB, Express, React (via Vite), and Node.js
Project Structure
/server- Express.js backend/client- React frontend (built with Vite)
Prerequisites
- Node.js (stable version)
- npm
- Docker and Docker Compose (for running the database services)
The development environment is configured to run MongoDB, Redis, and MySQL
as Docker containers. See the .devcontainer/docker-compose.yml file for
details.
Note on Database Usage: While the Dev Container provides MongoDB, Redis,
and MySQL services, the current server application (server/server.js) is
only wired up to connect to and use MongoDB. To utilize Redis or MySQL,
you will need to:
- Install the appropriate Node.js client libraries (e.g.,
ioredisfor
Redis,mysql2for MySQL) in theserver/package.json. - Add connection logic and data interaction code to your server
application.
Getting Started
You can get started with this template in several ways:
- Open a Codespace directly from GitHub:
- If this repository is public, you can click the "Code" button on the
repo page and select "Create codespace on main" (or another branch).
No need to fork or clone first — GitHub will automatically provision a
Codespace with the repo contents and devcontainer setup.
- If this repository is public, you can click the "Code" button on the
- Fork the repository:
- If you want your own copy of the repository (for making changes or
keeping your own Codespace), fork it on GitHub, then open a Codespace
from your fork.
- If you want your own copy of the repository (for making changes or
- Manual setup using the devcontainer:
- You may also copy the
.devcontainerfolder/configuration to another
project or environment to reuse the same development setup. This is
only recommended if you know what you are doing — your project may not
work at all if the environment is not compatible or is missing
required services.
- You may also copy the
Installation
- If you forked, cloned, or opened the repository in a Codespace, install
dependencies:
npm run install-allThis will install dependencies for the root project, client, and server.
Running the Development Environment
To run both the client and server concurrently:
npm run devTo run only the client:
npm run clientTo run only the server:
npm run serverNote (Codespaces Ports): There is a known GitHub Codespaces issue where port 3000 may not serve even when set to Public — so open the Ports panel, locate port 3000, toggle Public/Private, and refresh your browser or preview.
Building for Production
To build the client:
npm run buildEnvironment Variables
This project uses a dual-configuration approach for environment variables:
config.env
Server default configuration is stored in /server/config.env and can be
committed to version control:
PORT: Server port (default: 5000)MONGO_URI: MongoDB connection string for developmentNODE_ENV: Environment (development/production)
.env (when needed)
For sensitive information such as API keys or secrets that are not
defined in the committed config.env file, create a .env file in the
/server directory. This file is not committed to version control
(ensure it's in server/.gitignore).
.env is used to provide variables (primarily secrets) that are not
already present in config.env or the system environment. If a variable
is defined in config.env, its value from config.env will be used,
and it will not be affected or overridden by .env.
Typically, your server/.env file would contain items like:
- Secret keys for signing JWTs (e.g.,
JWT_SECRET). - API keys for external services (e.g.,
THIRD_PARTY_API_KEY). - Production-specific database credentials or connection strings if they
are entirely separate from any defaults inconfig.envand are
considered secret.
How .env and config.env interact:
- Variables from
config.envare loaded first. - Then, variables from
.envare loaded.- If a variable is defined in both
config.envand.env, the
value fromconfig.envwill be used. .envis primarily used to add variables that are not present in
config.env(e.g., secret keys, production-specific database
URIs that don't have a default inconfig.env).
- If a variable is defined in both
Security Best Practices
Note: For development with real credentials or production
deployment:
- Ensure
.envfiles are added to.gitignore - Never commit files containing real credentials to version control
- Consider using a secrets manager for production deployments
- Implement proper authentication for database connections
- Sanitize all user inputs to prevent injection attacks
- Use HTTPS in production environments
Database Connections
This application is configured to connect to:
- MongoDB at
mongodb://mongo:27017/mern_app - Redis at
redis://redis:6379 - MySQL at
mysql:3306(not used in this starter but available)
All databases are running in Docker containers with persistent storage.
Code Quality, Linting, Formatting, and Security
Both the frontend (client) and backend (server) are equipped with robust linting, formatting, and security tooling:
- Linting:
- Run
npm run lintin either theclientorserverdirectory to check for code style and quality issues. - Run
npm run lint:fixto automatically fix many linting issues.
- Run
- Formatting:
- Run
npm run formatin either theclientorserverdirectory to automatically format code using Prettier.
- Run
- Vulnerability Checks:
- Run
npm auditin either theclientorserverdirectory to check for security vulnerabilities in dependencies. - Run
npm audit fixto attempt to automatically fix any found vulnerabilities.
- Run
These scripts help maintain code quality, consistency, and security across the project. It is recommended to run these checks regularly, especially before committing or deploying code.
- ESLint (
eslint.config.js):- Client (React/Vite): Utilizes
eslint-plugin-reactfor
React-specific rules (including hooks and JSX best practices),
eslint-plugin-react-refreshfor Vite integration, andglobals
for browser environment. Prettier violations are treated as errors. - Server (Node.js/Express): Employs
eslint-plugin-nfor Node.js
specific rules andglobalsfor the Node.js environment. Prettier
violations are treated as warnings. - Both use
@eslint/jsrecommended rules as a base.
- Client (React/Vite): Utilizes
- Prettier: Integrated via
eslint-plugin-prettierand
eslint-config-prettierto handle all code formatting, ensuring a
consistent style across the codebase. Formatting can be applied using
npm run formatin bothclientandserverdirectories. - Consistency: This setup helps maintain code quality, catch errors
early, and enforce a uniform coding style throughout the project.
