GithubHelp home page GithubHelp logo

miniroute's Introduction

miniroute

miniroute is a simple routing library with the following features:

  • Named parameters
    • /blog/:category/:title
    • /asset/*path
  • Middleware support (basicauth, ratelimit and custom functions)
  • No external dependencies (uses default http and reflect modules)

Example

import miniroute { Router }
import mime

var router = Router()
router.get('/', |request, response| {
  response.headers['Content-Type'] = 'text/html'
  response.write('<h1>Index!</h1>')
})

/* Wilcard Routes */
router.get('/assets/*path', |request, response| {
  var path = request.params['path']
  var type = mime.detect_from_name(path)

  response.headers['Content-Type'] = 'text/html'
  response.write('<h1>Asset ${path} has mimetype ${type}</h1>')
})

/* Named Routes */
router.get('/:firstname/:lastname', |request, response| {
  var firstname = request.params['firstname']
  var lastname  = request.params['lastname']

  response.headers['Content-Type'] = 'text/html'
  response.write('<h1>Hello ${firstname} ${lastname}!</h1>')
})

router.serve(3000)
  1. localhost:3000/ will show Index!.
  2. localhost:3000/assets/css/style.css will show Asset css/style.css has mimetype text/css
  3. localhost:3000/John/Smith will show Hello John Smith!

Wildcard parameters always have to be at the end of the URL path.

Middleware

Middleware is supported to allow for easy logging, verification, etc.

If your middleware function returns false, then it won't continue to the next method.

import miniroute { * } # { Router, middleware }

def log(request, response) {
  echo '${request.ip} => ${request.path}'
}

def homepage(request, response) {
  response.headers['Content-Type'] = 'text/html'
  response.write('<h1>Welcome!</h1>')
}

var router = Router()

# Global middleware (logging for all routes)
router.use(log)

# Basic auth middleware, login with username "foo" and password "bar"
router.get('/', middleware.basicauth({'foo': 'bar'}), homepage)

router.serve(3000)

Using router.use() adds middleware to every single server request. So in the example above, every request is being printed to the console.

There's also some built-in middleware like basicauth() and ratelimit() making it easy to add login authentication and more security to the module.

miniroute's People

Stargazers

 avatar

Watchers

 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.