Skip to main content

Entry Point

Plumier application is the entry point of all Plumier based API. Creating Plumier application simply by creating instance of Plumier class. The simplest Plumier application at least must have WebApiFacility installed like below.

import Plumier from "plumier"
new Plumier()
.set(new WebApiFacility())
.listen(8000)

By default Plumier will search for controllers using this glob pattern ./**/*controller.+(ts|js) or ./**/*entity.+(ts|js), its mean it will search for all TypeScript/JavaScript files ends with controller or entity (for first class entity). It also automatically detected exported controller in the same file with the entry point file, this is useful for single file project.

Above configuration does noting because no controller specified. We can start above code using ts-node or ts-node-dev, by specify it on the package.json script like below.

{
"name": "my-cool-api",
"version": "1.0.0",
"scripts": {
"debug": "ts-node-dev --inspect -- src/index",
"build": "tsc",
"start": "node src/index"
},
"dependencies": {
"plumier": "^1.0.0-rc.7",
},
"devDependencies": {
"ts-node-dev": "^1.1.1",
"typescript": "^4.1.3",
}
}

Facilities#

Plumier application is configurable using facility, facility is a framework component consist of configurations, ordered middlewares and some micro process before the framework started.

There are list of built-in facility that ready to be used.

FacilityIncludesPackage
WebApiFacilityBody parser, CORS middleware, Default dependency resolverplumier
RestApiFacilitySame as WebApiFacility except its provided more strict restful API status codeplumier
ControllerFacilityHost controllers by path or type, furthermore controllers can be grouped and versionedplumier
LoggerFacilitySimple request logging and error reportingplumier
JwtAuthFacilityJwt middleware, Enable authorization, Jwt Secret configuration@plumier/jwt
MongooseFacilityMongoose schema generator, generic controller and connection management@plumier/mongoose
TypeORMFacilityProvided helper and generic controller for TypeORM@plumier/typeorm
ServeStaticFacilityServe static files@plumier/serve-static
SwaggerFacilityServe Swagger UI and generate Open API 3.0 automatically@plumier/swagger
FacebookOAuthFacilityProvide Facebook specific social login functionalities@plumier/social-login
GoogleOAuthFacilityProvide Google specific social login functionalities@plumier/social-login
GitHubOAuthFacilityProvide GitHub specific social login functionalities@plumier/social-login
GitLabOAuthFacilityProvide GitLab specific social login functionalities@plumier/social-login
TwitterOAuthFacilityProvide Twitter specific social login functionalities@plumier/social-login

Application For Testing#

When Plumier listen method called immediately its make Plumier lost its ability for testing purpose. To do that you can use initialize method to get a testable server like below.

import Plumier, { Configuration, WebApiFacility } from "plumier"
function createApp(config?: Partial<Configuration>) {
return new Plumier()
// specify the rootDir since it will be executed in different directory (test dir)
.set({ ...config, rootDir: __dirname })
.set(new WebApiFacility())
.initialize()
}
export default createApp

By using code above, you can test your API using supertest like below.

import createApp from "../src/app"
import supertest from "supertest"
it("Should serve API properly", async () => {
const app = await createApp({ mode: "production" })
await supertest(app.callback())
.get("/")
.expect(200)
})