Table of contents
No headings in the article.
Building a RESTful API is a common task in web development, especially for creating applications that require data exchange between different systems or devices. Node.js is a popular platform for building scalable and efficient backends for web applications. In this tutorial, we will focus on using Exclusivejs, a lightweight and flexible Node.js framework, to build a RESTful API. One of the benefits of Exclusivejs is that it provides a simple and intuitive way to define routes for handling HTTP requests. We will explore how to create routes and handle different types of requests, such as GET, POST, PUT, and DELETE, using Exclusivejs. Additionally, we will cover how to structure our application's routes in a modular and maintainable way, using techniques such as route nesting and separating routes into different files. By the end of this tutorial, you will have a solid understanding of how to use Exclusivejs to build a robust and scalable API for your web application.
Another advantage of Exclusivejs is its support for dependency injection, which makes it easier to manage and maintain our application's dependencies. By using dependency injection, we can easily define and manage our application's dependencies, making it more modular and testable. We will see how to use Exclusivejs's built-in dependency injection system to inject dependencies into our application's routes and controllers, making our code more organized and maintainable.
command to install the package
npm i exclusivejs --save
To initialize the Exclusivejs framework, we start by importing the library using require("exclusivejs").instance()
. Once imported, we can use the init()
method to start the server. By default, the server runs on port 1234, which can be overridden by providing a new port number in the .env
file.
const Exclusivejs = require("exclusivejs").instance()
Exclusivejs.init()
This automatically springs up a server with a prefix of "/api". If you want to change the prefix, you can call the .setApiPrefix() method and pass in your desired API prefix as an argument.
const Exclusivejs = require("exclusivejs").instance()
Exclusivejs
.setApiPrefix("your api route prefix") // api/v1
.init()
This will appear on your console that a server has been started.
In order to define a new route in Exclusivejs, we need to create a new JavaScript file or a folder containing a JavaScript file within our routes directory, which is located in the src directory by default. For example, we can create an index.js file to define our routes. In this file, we can use the Exclusivejs method for defining routes to specify the desired HTTP method and route URL. This will automatically create a new route in our application.
- project-folder/
-src
- routes/
- index.js
Exclusivejs allows you to specify the location of your routes folder. By default, it expects your route files to be created in the src/routes directory. However, you can specify a different location using the setRoutePath() method. This provides the flexibility to organize your routes in a way that makes sense for your specific application.
const Exclusivejs = require("exclusivejs").instance()
Exclusivejs
.setApiPrefix("your api route prefix") // api/v1
.setRoutePath("your routes folder") //route
.init()
To create a route in Exclusivejs, we first need to create a new file or folder containing a JavaScript file in our routes directory. This is typically done within the src/routes
folder by convention. In the newly created file, we can define our route methods, such as GET, POST, PUT, and DELETE, by implementing the corresponding HTTP methods.
For instance, we can define a TestRoute
class with four methods for each HTTP verb. Once defined, we can export the TestRoute
class from the file by setting it as the route
property of the module.exports
object.
When the application is started, Exclusivejs will automatically discover and register all the routes defined in the src/routes
folder. This means that if we create a new endpoint such as /api/users
in the TestRoute
class, we can access it by sending a GET request to http://localhost:1234/api/users
. We can similarly create endpoints for other HTTP verbs such as PUT, POST, and DELETE by defining their respective methods in our route file.
Once the server starts, Exclusivejs will log all the registered routes in the console, which can be helpful for debugging and verification.
The route file will look like
class TestRoute {
get = (request, response) => {
response.send("GET request to /api");
}
post = (request, response) => {
response.send("POST request to /api");
}
put = (request, response) => {
response.send("PUT request to /api");
}
patch = (request, response) => {
response.send("PATCH request to /api");
}
delete = (request, response) => {
response.send("DELETE request to /api");
}
}
module.exports = {
route: TestRoute
}
An example of how to create multiple routes in the existing route is to update the index.js file in the routes folder. We can define additional methods and routes by adding new properties to the class. We can add additional routes to the TestRoute class by specifying the desired HTTP method and route URL using dot notation. For example, to create a new route for GET requests to the '/api/user' URL, we can define a new method in the TestRoute class as follows:
class TestRoute {
get = (request, response) => {
response.send("GET request to /api");
}
post = (request, response) => {
response.send("POST request to /api");
}
put = (request, response) => {
response.send("PUT request to /api");
}
patch = (request, response) => {
response.send("PATCH request to /api");
}
delete = (request, response) => {
response.send("DELETE request to /api");
}
'get.user' = (request, response) => {
response.send("GET request to /api/user");
}
'delete.user' = (request, response) => {
response.send("DELETE request to /api/user");
}
'put.user' = (request, response) => {
response.send("PUT request to /api/user");
}
'get.product' = (request, response) => {
response.send("GET request to /api/product");
}
}
module.exports = {
route: TestRoute
}
In the updated TestRoute class, we added new methods to handle GET, DELETE, and PUT requests to the '/api/user' URL, as well as a new method to handle GET requests to the '/api/product' URL. These new routes can be accessed using dot notation, such as 'get.user', 'delete.user', 'put.user', and 'get.product'.
This will generate a new route and it will appear in the console
To organize our routes better, we can create a folder named "user" inside our "routes" folder and an "index.js" file inside the "user" folder. Then we can move all the user-related routes inside the "index.js" file in the "user" folder. The new file structure will look like:
routes
|- index.js
|- user
|- index.js
In the "routes/index.js" file, we will keep the non-user related routes:
class TestRoute {
get = (request, response) => {
response.send("GET request to /api");
}
post = (request, response) => {
response.send("POST request to /api");
}
put = (request, response) => {
response.send("PUT request to /api");
}
patch = (request, response) => {
response.send("PATCH request to /api");
}
delete = (request, response) => {
response.send("DELETE request to /api");
}
}
module.exports = {
route: TestRoute
}
and our user routes/user/index.js will look like
class UserRoute {
"get:user" = (request, response) => {
response.send("GET request to /api/user");
}
delete = (request, response) => {
response.send("DELETE request to /api/user");
}
put = (request, response) => {
response.send("PUT request to /api/user");
}
patch = (request, response) => {
response.send("PATCH request to /api/user");
}
}
module.exports = {
route: UserRoute
}
If we need to create a route with a parameter such as /api/v1/user:id
, we can create it by defining a function with the name get.user:id
, post.user:id
, put.user:id
, patch.user:id
, or delete.user:id
, depending on the HTTP method we want to use.
the updated user route file will look like
class UserRoute {
"get:user" = (request, response) => {
response.send("GET request to /api/user");
}
delete = (request, response) => {
response.send("DELETE request to /api/user");
}
put = (request, response) => {
response.send("PUT request to /api/user");
}
patch = (request, response) => {
response.send("PATCH request to /api/user");
}
"get:id" = (request, response) => {
response.send("GET request to /api/v1/user/:id");
}
"post:id" = (request, response) => {
response.send("POST request to /api/v1/user/:id");
}
"put:id" = (request, response) => {
response.send("PUT request to /api/v1/user/:id");
}
"patch:id" = (request, response) => {
response.send("PATCH request to /api/v1/user/:id");
}
"delete:id" = (request, response) => {
response.send("DELETE request to /api/v1/user/:id");
}
}
module.exports = {
route: UserRoute
}
To avoid cluttering our UserRoute route files with parameter-specific routes, Instead of defining route parameters directly in the route files, we can create a folder with the name of the parameter we want to use, for example, [id]. Inside this folder, we can create an index.js file that contains the methods (GET, POST, etc.) that we want to associate with this parameter. This way, any requests to a route that includes this parameter will be handled by the methods defined in this folder. For example, if we have a route like /api/v1/user/:id, we can create a folder called [id], and inside it, an index.js file that contains the methods "GET", "POST", "PUT", "PATCH", or "DELETE" that we want to associate with this parameter.
routes
|- index.js
|- user
|- index.js
|- [id]
|- index.js
the [id]/index.js file will now look like
class UserIdRoute {
get = (request, response) => {
const id = request.params.id;
response.send(`GET request to /api/user/${id}`);
}
delete = (request, response) => {
const id = request.params.id;
response.send(`DELETE request to /api/user/${id}`);
}
put = (request, response) => {
const id = request.params.id;
response.send(`PUT request to /api/user/${id}`);
}
patch = (request, response) => {
const id = request.params.id;
response.send(`PATCH request to /api/user/${id}`);
}
}
module.exports = {
route: UserIdRoute
}
Thank you for taking the time to read through this write-up on building RESTful APIs with Node.js and Express.js. I hope you found it informative and helpful. If you have any questions or comments, feel free to reach out to me. Your feedback is always appreciated and helps me to improve my work.
In the next section, we will be talking about dependency injection in ExclusiveJS, a powerful framework for building Node.js applications. We will explore how dependency injection can help us manage dependencies, reduce coupling, and improve the testability and maintainability of our code.
Thanks again for your interest, and have a great day!
Below you will find the link the what we have been doing