https://github.com/trekjs/trek
๐ Fast Async Web Framework For Modern Node.js
https://github.com/trekjs/trek
Last synced: 4 months ago
JSON representation
๐ Fast Async Web Framework For Modern Node.js
- Host: GitHub
- URL: https://github.com/trekjs/trek
- Owner: trekjs
- License: mit
- Created: 2014-05-11T07:02:16.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-02-01T13:39:56.000Z (almost 8 years ago)
- Last Synced: 2024-11-28T03:33:14.545Z (about 1 year ago)
- Language: JavaScript
- Homepage: https://trekjs.com
- Size: 853 KB
- Stars: 161
- Watchers: 9
- Forks: 12
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Features
* **Elegant**. Use `async` and `await` for asynchronous programs
* **Fast**. 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
```console
$ npm install trek --save
```
## Examples
### [Hello Trek](examples/hello-world/index.js)
The lightweight app uses with **Engine**. Likes **Koa**.
```js
const { Engine: Trek, Router } = require('../../lib')
async function launch() {
const app = new Trek()
const router = new Router()
router.add('GET', '/', async ({ res }) => {
res.send(200, 'Hello, Trek!')
})
router.add('GET', '/startrek', async ({ res }) => {
res.type = 'html'
res.send(200, Buffer.from('Hello, Star Trek!'))
})
router.add('POST', '/', async ({ res }) => {
res.send(200, {
status: 'ok',
message: 'success'
})
})
app.use(async ({ req, res }, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log(`${ms}ms`)
})
app.use(async ({ req, res }, next) => {
const route = router.find(req.method, req.path)
if (route) {
const [handler] = route
if (handler !== undefined) {
await handler({ req, res })
return
}
}
await next()
})
app.use(async ({ res }) => {
res.status = 404
res.end()
})
app.run(3000)
}
launch().catch(console.error)
```
### [Star Trek](examples/startrek/app.js)
The richer app, customize and expand your app.
```js
const Trek = require('../../lib')
async function launch() {
const app = new Trek()
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' })
await app.bootUp()
app.use(async ({ logger, rawReq, rawRes }, next) => {
logger.info(rawReq)
await next()
logger.info(rawRes)
})
app.use(async ({ cookies }, next) => {
cookies.set('name', 'trek')
await next()
})
app.use(ctx => {
if (ctx.req.path === '/') {
return ctx.res.send(200, 'Star Trek!')
} else if (ctx.req.path === '/error') {
throw new Error('Nothing')
}
// Something else return 404
ctx.cookies.set('name', null)
ctx.res.send(404)
})
app.on('error', err => {
app.logger.error(err)
})
await app.run(3000)
}
launch().catch(console.error)
```
---
> [fundon.me](https://fundon.me) ย ยทย
> GitHub [@fundon](https://github.com/fundon) ย ยทย
> Twitter [@_fundon](https://twitter.com/_fundon)
[trek]: https://trekjs.com/
[express.js]: http://expressjs.com
[koa.js]: http://koajs.com
[middleware]: https://github.com/trekjs/middleware
[router]: https://github.com/trekjs/router