lexpress
Light Express framework for NodeJS.
Description
Lexpress is a featherlight microframework based on Express providing a ready-to-use base controller inluding :
- A JSON Schema validation for GET, POST, PUT and DELETE requests.
- An easy-to-use cache middleware.
- A clean JSON error response.
Typescript definitions are also included.
Getting Started
Installation
npm i lexpress
Basic Web & Api Example
index.ts
import { Lexpress } from 'lexpress'
import routes from './routes'
const lexpress = new Lexpress({
routes,
viewsEngine: 'pug',
viewsPath: 'src/views'
})
lexpress.start()
src/routes.ts
import { Route } from 'lexpress'
import ApiHelloWorldController from './controllers/api/HelloYouController'
import WebHelloWorldController from './controllers/web/HelloYouController'
const routes: Route[] = [
{
path: '/:name',
controller: WebHelloWorldController,
method: 'get',
},
{
path: '/api/hello',
controller: ApiHelloWorldController,
method: 'get',
}
]
export default routes
src/controllers/api/HelloYouController.ts
import { BaseController } from 'lexpress'
export default class HelloYouController extends BaseController {
public get() {
const schema = {
type: 'object',
properties: {
email: {
type: 'string',
minLength: 1,
},
},
required: ['name'],
}
this.validateJsonSchema(schema, () => {
this.res
.status(200)
// Let's keep this response in cache for 24h
.cache(86400000)
.json({
message: `Hello ${this.req.query.name} !`,
name: this.req.query.name,
})
})
}
}
src/controllers/web/HelloYouController.ts
import { BaseController } from 'lexpress'
export default class HelloYouController extends BaseController {
public get() {
this.res
// Let's keep this response in cache for 24h
.cache(86400000)
.render('hello-you', {
name: this.req.params.name,
})
}
}
src/views/hello-you.pug
h1 Hello #{name} !