This repository is a ready-to-use starter kit for Deno, offering a well-organized and structured API setup similar to what you might expect from a Node.js environment. It includes configurations for essential packages such as Hono, Mongoose, and jsonwebtoken.
This starter kit is not intended for newcomers. It is recommended for developers who have advanced experience with Node.js and TypeScript, and who understand the Deno architecture. If you are new to these technologies, it is advisable to first familiarize yourself with the basics before using this starter kit.
- Name: Mr. Wayne
- Website: a4arpon.me
- GitHub: a4arpon
- LinkedIn: a4arpon
- Facebook: a4arpon
- Deno
- Hono
- Mongoose
- jsonwebtoken
This starter kit solves the problem of having an unstructured and disorganized API setup in Deno. Unlike Node.js, there isn't a wealth of well-organized starter templates for Deno. This kit provides:
- A structured folder organization
- Pre-configured middlewares for logging, CORS, and security
- A ready-to-use database connection setup with Mongoose
- Standardized response and error handling mechanisms
- Flexibility to add plugins and change database drivers as needed
I'm willing to include these things in the Starter-Kit in future
- Deno KV
- Deno Corn
- Mail System
- Authorization Guards
.
├── deno.json # Deno configuration file
├── deno.lock # Lock file for dependencies
├── README.md # Project documentation
└── src
├── config # Configuration files
│ └── helmet.config.ts
├── main.ts # Main application entry point
├── models # Database models
│ └── user.model.ts
├── routes # API route definitions
│ ├── router.ts
│ └── stable.routes.ts
├── middlewares # Application guards and middlewares
├── services # Business logic and services
│ └── users.services.ts
├── types # Type definitions
│ └── shared.types.ts
└── utils # Utility functions
├── async.ts
└── response.ts
MONGODB_URL=""
- Environment Loader: Loads environment variables.
- Middlewares: Sets up logging, CORS, and security headers.
- Database Connection: Connects to MongoDB using Mongoose.
- Router Setup: Defines base routes and integrates with the router.
- Stable Routes: Handles stable API routes.
- Beta Routes: Placeholder for beta routes, currently returns a not active message.
Defines the routes for user-related operations.
Contains the business logic for user operations, such as creating a user and retrieving users.
-
Async Handler (
async.ts
): Safely handles async operations to catch errors.export function safeAsync(func) { return async (context) => { try { return await func(context) } catch (error) { return exception( context, HTTPStatus.InternalServerError, "Internal Server Error", ) } } }
Explanation: This function wraps asynchronous operations to handle errors gracefully. It catches any errors thrown within the wrapped function and returns a standardized error response.
-
Response (
response.ts
): Standardizes JSON responses.export function response(context, message, data, extra, success) { return context.json({ success: success || true, message, ...extra, data, }) }
Explanation: This function formats the response sent to the client. It ensures a consistent response structure with optional additional data and success status.
-
Exception (
response.ts
): Handles exceptions and sends error responses.export function exception(context, status, error) { if (status && error instanceof Error) { context.status(status) return context.json({ success: false, status: status, message: error.message, }) } context.status(500) return context.json({ success: false, status: 500, message: "Internal Server Error", }) }
Explanation: This function handles exceptions and sends a structured error response. It sets the HTTP status code and returns an error message.
You can easily add plugins to this starter kit from the Hono website. Follow the instructions on the website to integrate additional functionalities as per your requirements.
The starter kit uses Mongoose for MongoDB. However, you can replace it with any
other database driver by updating the database connection logic in main.ts
and
adjusting the models and services accordingly.
-
Clone the repository:
git clone https://github.com/a4arpon/deno-starter-kit.git
-
Install Deno: Follow the instructions on the official Deno website.
-
Run the application:
deno task start
Contributions are welcome. Please create an issue or submit a pull request on GitHub.