Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

can-route

Package Overview
Dependencies
Maintainers
1
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

can-route

A simple regexp router that runs inside a `http.createServer` handler and returns false when it can’t route a request.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4K
increased by10%
Maintainers
1
Weekly downloads
 
Created
Source

can-route

can-route is a simple router that runs inside a http.createServer handler and returns false when it can’t route a request. Its routes are defined with named regular expressions.

Build status

Browser support

Install

npm install can-route

Example

var http = require('http')
var can = require('can-route')()

// Home
can.get(/^\/$/,
  function(req, res) {
    res.end('Welcome to the homepage\n')
  }
)

// Item
can.patch(/^\/(:<id>[a-f0-9]{16})\/?$/i,
  function(req, res, params) {
    res.end(params.id + '\n')
  }
)

// Server
http.createServer(function(req, res) {
  if (!can.route(req, res)) {
    res.statusCode = 404
    res.end()
  }
}).listen(8080)
Browser

can-route also works in the browser with basically the same API:

var can = require('can-route')()

can.get(/^\/$/, function() {
  // No arguments for routes without parameters.
})

can.get(/^\/(:<name>[a-z]+)\/?$/i, function(params) {
  // The only possible argument is the param object.
})

can.get(/#\/(:<name>[a-z]+)\/$/i, function(params) {
  // It's possible to define routes based on
  // the value of location.hash.
})

window.onpopstate = function() {
  var path = window.location || '/path/to/page'
  if (!can.route(path)) {
    // Handle 404
  }
}

window.onhashchange = function() {
  var includeQueryAndHash = true
  if (!can.route(window.location, includeQueryAndHash)) {
    // Handle 404
  }
}

License

MIT

Keywords

FAQs

Package last updated on 15 Nov 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc