Giter Club home page Giter Club logo

trek's Introduction

Trek

Trek.js

Fast Async Web Framework For Modern Node.js

Build status Codecov NPM version MIT License

Features

  • Elegant. Use async and await for asynchronous programs

  • Fast. High performance middleware and router

  • Modern. ES6+, only for Node.js v6

  • Flexible. Modular and extensible

  • Amiable. Similar to Express.js and Koa.js

Installation

$ npm install trek@next

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.initialize()

  // 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(err => console.error(err))

License

MIT

trek's People

Contributors

fundon avatar greenkeeperio-bot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.