Creating Rest API on BUN with ElysiaJS
Dec 28, 2023
0
What is ElysiaJS?#
Elysia is built with simplicity and type-safety in mind. Highly performant and Optimized for Bun. Allowing developers to create server with familiar APIs like Express and Fastify. Bun is a lightweight and fast JavaScript runtime aiming to be a drop-in replacement for Node.js, resulting in performance benefits.
In this article, we will create a simple REST API using ElysiaJS and Bun. We will also use MongoDB as our database.
Installation
You need to install Bun in your system so that we can use it to create our project.
Create a new project
This will create a new project named rest-api
in the current directory.
Install dependencies
We will use mongoose
as our database driver. You can use any other driver as well.
Create a model#
Create a new file rest-api/src/models/user-model.ts
with the following code:
Here we have created a simple model for our user. We will use this model to create, update, delete and get users from our database.
ElysiaJS with Next.JS and React Query
Elysia is an ergonomic web framework for building backend servers. Fully Type safe RPC which makes fun building backend and API using elysia.
Read Full PostCreate a controller#
Create a new file rest-api/src/controllers/user-controller.ts
with the following code:
Here we created a controller for our user model. We implemented all the CRUD operations for our user model.
Obviously for good approach you can create a separate routes file and import your controller there. also for error handling you can use try/catch
block.
Create a server#
Move to app/src/index.ts
file with the following code:
Above we created a server and connected to our database. We also used our user controller and passed to Elysia middleware.
Implement Authentication for our API#
We will use simple key based authentication for our API. But you can use any other authentication method as well as the process will be same.
This is how our complete server code will look like:
We used bearer plugin which is provided by Elysia. After that we used onBeforeHandle
method which will run first where we are checking authentication for our API.
We are passing 12345678
as Bearar token in our request header. and we are checking if the token is valid or not. By default, Elysia in case of invalid token will return 404
status code with Unauthorized
message.
Conclusion#
In this article, we created a simple REST API using ElysiaJS and Bun. We also implemented authentication for our API. You can find the complete code on Github
That's it for this article. I hope you found it useful. If you have any questions or feedback, please share in comment section. I will be happy to answer them.
Comments (0)