Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
arrow-express
Advanced tools
Boostrap your express application with ease.
With arrow-express
you can easily configure your express application routes.
What arrow-express
offers:
RequestError
to send back desired response with code.What arrow-express
doesn't offer:
/**
* Configure express application with arrow-express library.
* GET /user/:id
* GET /users
*/
import Express from "express";
import { Application, Controller, Route } from "arrow-express";
const ExpressApp = Express();
function start() {
const application = Application({
app: ExpressApp,
})
.registerController(usersController)
.configure();
ExpressApp.listen(3000);
}
const usersController = Controller()
.prefix("user")
.handler(authorizeUser)
.registerRoutes(getUserByIdRoute, getUsersRoute);
const getUserByIdRoute = Route()
.method("get")
.path(":id")
.handler(async (req, res, authorizedUser) => {
// ...
});
const getUsersRoute = Route()
.method("get")
.path("")
.handler(async (req, res, authorizedUser) => {
// ...
});
function authorizeUser(req, res) {
// Authorize user
return { id: 1, name: "John Doe" };
}
To install package use command:
npm install arrow-express
Point of start for every application. It is used to register controllers and routes.
import Express from "express";
import { Application, Controller, Route } from "arrow-express";
const ExpressApp = Express();
const application = Application({
app: ExpressApp,
})
.registerController(
Controller()
.prefix("user")
.registerRoute(
Route()
.method("get")
.handle(async (req, res) => {
// get user and response
})
)
)
.configure();
ExpressApp.listen(3000);
registerController
- register controller in application.configure
- register routes in express app.Controller is used to manage group of routes under one prefix route.
import { Application, Controller } from "arrow-express";
function UserController() {
return Controller()
.prefix("user")
.registerRoute(
Route()
.method("get")
.handle((req, res) => {
// get user and response
})
);
}
Application({
app: ExpressApp,
})
.registerControllers(UserController())
.configure();
// Registered path will be: GET '/user'
handler(handler)
- register controller handler which will be used by all routesprefix(prefix)
- register controller prefix which will be used by all routesregisterRoute(route)
- register route in controllerregisterRoutes(...routes)
- register multiple routes in controllerregisterController(controller)
- register sub controller in controllerregisterControllers(...controllers)
- register multiple sub controllers in controllerController handler can be used to eg: authorize user and get it's context which will be passed to routes. Handlers like controllers can be chained.
Route is used to manage route handling.
import { Application, Controller, Route } from "arrow-express";
Application({
app: ExpressApp,
})
.registerController(
Controller()
.prefix("user")
.registerRoutes(
Route()
.method("get")
.path("myself")
.handler(async (req: Express.Request, res: Express.Response) => {
const user = {};
// Use some service to extract route
return user;
})
)
)
.configure();
// Registered path will be: GET '/user/myself'
method
- register method used for routepath
- register path of route alongside with prefix it is used to create full pathhandler
- set request handler, here you can handle requestRoute handler receive 3 arguments:
request
- which is Express.Request for pathresponse
- which is Express.Responsecontext
- which is resolution of controller's handlerFeatures of route handler:
res
then library won't try to send result pf handler.If route handler throws RequestError
it will be handled by arrow-express
and respond with http code and response object.
import { RequestError } from "arrow-express";
throw new RequestError(401, {
code: 401,
message: "Unauthorized",
});
Check out example
folder for example code guidance.
Good approach is to use function closures to organize code into chunks.
Eg: create function which will return Controller
and pass to it instance of service as argument instead of importing Singleton service.
This way you will be able to test routes and controllers with ease without module mocking and you will avoid side effects.
// index.ts file
async function startServer() {
const expressApplication = Express();
const userService = new UserService();
Application({
app: expressApplication,
})
.registerController(UserController(userService))
.configure();
expressApplication.listen(3000);
}
// user.controller.ts file
export function UserController(userService: UserService): ControllerConfiguration {
return Controller().prefix("users").registerRoutes(GetUserById(userService), GetMyselfRoute(userService));
}
FAQs
Library to bootstrap express applications with zero configuration
The npm package arrow-express receives a total of 444 weekly downloads. As such, arrow-express popularity was classified as not popular.
We found that arrow-express demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.