Ocajo Backend is a robust Node.js backend template designed for rapid development of scalable and secure web services. It incorporates best practices and a comprehensive set of tools to help you build reliable and maintainable backend applications.
- TypeScript: For type-safe code and improved developer experience
- Express.js: Fast, unopinionated, minimalist web framework for Node.js
- Prisma: Next-generation ORM for Node.js and TypeScript
- Zod: TypeScript-first schema validation with static type inference
- Sentry: Error tracking and performance monitoring
- Winston: Versatile logging library
- ESLint: Linting utility for JavaScript and TypeScript with security rules
- Mocha & Chai: Testing framework and assertion library
- Dotenv: Environment variable management
- Helmet: Helps secure Express apps by setting various HTTP headers
- Compression: Response compression middleware
- Rate Limiting: Protects against brute-force attacks
- In-Memory Caching: Improves performance for frequently accessed data
- Swagger: API documentation
-
app/
: Contains route handlers and Express routers -
lib/
: Houses shared libraries and configurations -
middlewares/
: Express middlewares -
utils/
: Utility functions and helpers -
server.ts
: Main application entry point
- Clone this repository
- Run
npm install
to install dependencies - Set up your
.env
file with necessary environment variables - Run
npx prisma migrate dev
to set up your database - Start the development server with
npm run dev
-
npm run dev
: Start the development server -
npm run build
: Build the TypeScript project -
npm start
: Start the production server -
npm test
: Run tests -
npm run lint
: Lint the codebase
Make sure to set up the following environment variables:
-
NODE_ENV
: Application environment (development, test, production) -
PORT
: Port number for the server -
DATABASE_URL
: PostgreSQL database connection string -
SENTRY_DSN
: Sentry Data Source Name for error tracking -
JWT_SECRET
: Secret key for JWT authentication
This project uses Mocha for testing. Write your tests in the src/app/**/*.test.ts
files. Run tests using npm test
.
Custom error handling is implemented using the error-handler.ts
middleware. It provides consistent error responses and integrates with Sentry for error tracking. Logging is handled by Winston, with different log levels for different environments.
The response.ts
utility provides standardized success and error responses for your API endpoints. This structure ensures consistency and predictability across all API responses.
- Consistency: Maintains a uniform API interface
- Predictability: Simplifies integration for frontend developers
- Error handling: Provides a standard way to communicate errors
- Metadata: Allows for additional information beyond just the data
All API responses follow this structure:
-
success
: Alwaystrue
for successful responses -
data
: The main payload of the response (optional) -
meta
: Additional metadata, like pagination (optional) -
upserted
: Object containing records of upserted entities (optional) -
deleted
: Object containing records of deleted entities (optional)
-
success
: Alwaysfalse
for error responses -
message
: A human-readable error message -
code
: An error code (e.g., BAD_REQUEST, UNAUTHORIZED) -
issues
: Object containing validation or more granular issues (optional)
This project includes several security measures:
- Helmet for setting secure HTTP headers
- Rate limiting to prevent abuse
- Security-focused ESLint rules to catch potential vulnerabilities during development
- CSRF protection (ensure to implement CSRF tokens in your frontend)
A simple in-memory caching mechanism is implemented using node-cache
. This improves performance for frequently accessed, relatively static data.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the ISC License.
To create a new project using this template, run the following command:
npx create-ocajo-app my-new-project
This will create a new directory called my-new-project
with all the necessary files and dependencies installed.
After the project is created:
-
Navigate to the project directory:
cd my-new-project
-
Set up your
.env
file with necessary environment variables. -
Run
npx prisma migrate dev
to set up your database. -
Start the development server:
npm run dev
Your new Ocajo backend project is now ready for development!