A light web server framework base Koa.
npm install @heroor/hero
yarn add @heroor/hero
pnpm install @heroor/hero
// index.ts
import Hero from '@heroor/hero'
const server = new Hero()
server.start(3000, console.log.bind(0, 'Server is running: http://localhost:3000/'))
Create router files in router/
folder, use decorators and export a Class
:
└── src
├── index.ts
└── router
└── user.ts <--- here!
// router/user.ts
import { get, Context } from '@heroor/hero'
export default class User {
@get('/get/user')
public async getUser(ctx: Context) {
ctx.body = {
ok: 1,
data: { name: 'hero' },
}
}
}
Create a web server by port.
Hero.start(port, callback?)
const server = new Hero()
server.start(3000, console.log.bind(null, 'Running in 3000...'))
Create a static server by path.
Hero.static(path, options?)
const server = new Hero()
server.static(resolve(__dirname, './public'))
Use middleware of koa.
Hero.use(Koa.Middleware)
const server = new Hero()
server.use(bodyParser())
@get(path)
: Create aGET
router.@post(path)
: Create aPOST
router.@del(path)
: Create aDELETE
router.@put(path)
: Create aPUT
router.@head(path)
: Create aHEAD
router.@options(path)
: Create aOPTIONS
router.@patch(path)
: Create aPATCH
router.
// router/user.ts
import { get, post, Context } from '@heroor/hero'
export default class User {
@get('/get/user')
public async getUser(ctx: Context) {
// ...
}
@post('/create/user')
public async createUser(ctx: Context) {
// ...
}
}
MIT