Building RESTful APIs with Deno: A Boilerplate for Rapid Development
Tags: deno, typescript, node, javascript
Published: 2023-07-04 04:59:09

Hey, Dev.to community! Today, I'm excited to introduce you to Deno REST, a straightforward boilerplate project for building RESTful APIs using Deno, Oak, and deno_mongo. Deno is a secure runtime for JavaScript and TypeScript, Oak is a middleware framework for Deno's HTTP server, and deno_mongo is a MongoDB driver for Deno.

### Features
Deno REST comes packed with several useful features to help you jumpstart your API development:

- Organized by Model, Controller, Service structure
- MongoDB integration with deno_mongo
- JWT-based authentication
- User authorization with role-based access control
- CORS support for handling cross-origin requests
- Environment management via .env files
- Request validation to ensure valid data
- Graceful error handling
- Database seeding for initial data
- User roles and permissions management
- Password hashing with BCrypt
- Denon integration for automatic server restarts
- Integration tests for easy testing
- Docker and CircleCI integration for containerization and continuous integration

### Libraries Utilized
Deno REST leverages the following libraries:

- [Oak](https://deno.land/x/oak) - Middleware framework for Deno's net server
- [deno_mongo](https://deno.land/x/mongo) - MongoDB driver for Deno
- [cors](https://deno.land/x/cors) - CORS middleware for Deno
- [djwt](https://deno.land/x/djwt) - JSON Web Tokens in Deno, based on JWT and JWS specifications
- [yup](https://deno.land/x/yup) - Schema builder for value parsing and validation
- [bcrypt](https://deno.land/x/bcrypt) - OpenBSD Blowfish password hashing algorithm

### Getting Started
To get started with Deno REST, follow these steps:

1. **Install Deno**: [Instructions and official installation guide](https://deno.com/manual@v1.34.3/getting_started/installation#installation)
2. **Set up Environment Variables**: Review the `.environments/.env.example` file and create a suitable `.env` file based on your needs.
3. **Install Denon**: Follow the [Denon installation guide](https://deno.land/x/denon)
4. **Database Seeding**: Use the provided seeding script to populate the database with initial data:
    ```bash
    denon run --allow-read --allow-write --allow-net --unstable seed.ts
    ```
5. **Running the Project**: Start the project using the following command:
    ```bash
    denon start
    ```

### User Roles and Permissions
Customize user roles and permissions by modifying the `config/roles.ts` file.

### API Routes
Deno REST includes the following API routes:

- GET /users
- GET /users/:id
- POST /users
- PUT /users/:id
- DELETE /users/:id

Contributing and License:
Contributions to Deno REST are welcome! You can open issues or submit pull requests. The project is licensed under the MIT License.

Conclusion:
With Deno REST, you can kickstart your RESTful API development using Deno, Oak, and deno_mongo. It provides a clean architecture, essential features, and integration with popular libraries, allowing you to focus on building your API logic.

Give Deno REST a try and let us know your feedback! You can find the complete project and documentation on [GitHub](https://github.com/Prolifode/deno_rest).

Thank you for reading, and happy coding with Deno REST!