A Top-level framework of Express.js for developing clean architecture API service, especially on TypeScript.
Springpress provides basic Express.js functions and MVC utilities related out of the box. Let you deep-dive into the world of OOP and the scent of our motivation, Spring Boot with Springpress.
Node.js 16.15.0 (LTS) is required
Using npm:
$ npm install springpress
Using yarn:
$ yarn add springpress
Assume you are creating a dog controller that sends a response of the dog's name when requesting to /dog/name.
First, you need to create a main class that extends Springpress class. This class will store all of your things. e.g. services, controllers, repositories initialization.
import{Springpress}from'springpress';classExampleServerextendsSpringpress{publiconStartup(): Promise<void>{// This code will run when your start your application.// All of initialization should do it here.}}Don't forget to call Springpress#listen to bind to and to listen for conections. This method returns an http.Server object (the built-in HTTP module). You can use this instance in your testing framework to work with HTTP.
constport=3000;// you can specify whatever port you want :)constexampleServer=newExampleServer(port);exampleServer.listen();Easily define your controller with @ControllerMapping decorator:
@ControllerMapping('/dog')// <-- Decorator here!classDogControllerextendsController{// Routes implementation will be here.}The @ControllerMapping decorator will mount your class on a router with the path where you specified in the first decorator parameter. In this case, this class will be mapped with /dog on the router.
- A client can connect by
http://localhost:3000/dog/followed by your implemented route. (port 3000 is just an example) - On this step, you will not actually be able to access it. You need to implement a route first by following the next step.
Easily define your route with @RouteMapping decorator:
@ControllerMapping('/dog')classDogControllerextendsController{
@RouteMapping('/name',Methods.GET)// <-- Decorator here!privateasyncgetName(req: Request,res: Response){res.status(200).json({name: 'Doge',});}}The @RouteMapping decorator will mount your decorated method as a routing destination of an HTTP request in a controller.
A RouteMapping parameter
- path - a string of route path
- It can be anything that Express.js routing support
- method - an enum of HTTP method (You can use
Methodsimported from Springpress)
Now, your client will see http://localhost:3000/dog/name and get a response like below in a JSON format.
{
"name": "Doge"
}Everything above in the Controller section will not work if you haven't registered the controller in the Server class.
import{Springpress}from'springpress';import{DogController}from'./controllers/DogController';classExampleServerextendsSpringpress{publiconStartup(): Promise<void>{// Don't forget here!constcontrollerRegistry=this.getControllerRegistry();controllerRegistry.register(newDogController());}}There are many ways in which you can participate in this project, for example:
- Submit bugs and feature requests.
- Review source code changes.
- Fixing issues and contributing directly to the code base by submitting pull requests.
Copyright (c) Vectier. All rights reserved.
Licensed under the MIT license.