@hitachi567/core
v1.0.2
Published
basic interfaces, functions and classes for express app
Downloads
5
Readme
Core
hitachi567 <[email protected]>
Wrapper for express based app
Requirements
- node.js
- npm or yarn
Getting started
npm install @hitachi567/core
# or
yarn add @hitachi567/core
Usage
Initialize express app
import { initApp } from '@hitachi567/core'
const app = initApp({
cookieSecret: 'secret',
})
app.listen(5000, () => console.log('listening on 5000...'))
Define middleware
import Lib from '@hitachi567/core'
import { Router } from 'express'
interface Body {
username: string
password: string
}
interface Locals extends Lib.Locals {
hashedPassword: string
}
let tryPartOfMiddleware: Lib.Middleware<Body, Locals> = async (
request,
response,
next,
) => {
let hashedPassword = await Lib.Hashing.hashing(request.body.password)
response.locals.hashedPassword = hashedPassword
next()
}
let middleware1 = Lib.asyncMiddleware<Body, Locals>(tryPartOfMiddleware)
let middleware2: Lib.Middleware<Body, Locals> = async (
request,
response,
next,
) => {
try {
await tryPartOfMiddleware(request, response, next)
} catch (error) {
next(error)
}
}
function middleware3(): Lib.Middleware<Body, Locals> {
return Lib.asyncMiddleware<Body, Locals>(tryPartOfMiddleware)
}
// the expressions below are equivalent to each other
Router().use(middleware1)
Router().use(middleware2)
Router().use(middleware3())