trek
v0.0.1
Published
Fast Async Web Framework For Modern Node.js
Downloads
17
Maintainers
Readme
Features
Elegant. Use
async
andawait
for asynchronous programsFast. High performance middleware and router
Modern. ES6+, only for Node.js v8+
Flexible. Modular and extensible
Amiable. Similar to Express.js and Koa.js
Installation
$ npm install trek --save
Examples
Hello Trek
The lightweight app uses with Engine. Likes Koa.
import { Engine as Trek } from 'trek'
const app = new Trek()
app.use(async ({ res }, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log(`${ms}ms`)
res.end('Hello Trek!')
})
app.run(3000)
Star Trek
The richer app, customize and expand your app.
import Trek, { Router } from 'trek'
(async () => {
// router
const router = new Router()
router.get('/', async ({ res }) => {
res.send(200, 'Hello, Trek!')
})
router.get('/startrek', async ({ res }) => {
res.send(200, new Buffer('Hello, Star Trek!'))
})
router.post('/', async ({ res }) => {
res.send(200, {
status: 'ok',
message: 'success'
})
})
// app
const app = new Trek()
// customize paths of app
app.paths.set('app', { single: true })
app.paths.set('app/plugins', { glob: 'app/plugins/index.js', single: true })
app.paths.set('app/controllers', { glob: 'app/controllers/*.js' })
// autoload plugins
await app.bootUp()
// middleware
app.use(async ({ req, res }, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log(`${ms}ms`)
})
// work with router
app.use(async ({ req, res }, next) => {
const route = router.find(req.method, req.path)
if (route) {
const [handler] = route
if (handler !== undefined) {
return await handler({ req, res })
}
}
await next()
})
app.use(async ({ res }) => {
res.status = 404
res.end()
})
// start
await app.run(3000)
})().catch(console.error)