Giter Site home page Giter Site logo

urlrouter's Introduction

urlrouter Build Status Coverage Status Dependency Status

NPM

logo

http url router.

connect missing router middleware.

Support express format routing.

Support connect @1.8.x and @2.2.0+ .

Test connect version

  • 1.8.0+: 1.8.0 1.8.5 1.8.6 1.8.7
  • 1.9.0+
  • 2.2.0+
  • 2.3.0+
  • 2.4.0+
  • 2.7.0+
  • 2.8.0+
$ make test-all

Install

$ npm install urlrouter

Usage

Using with connect

var connect = require('connect');
var urlrouter = require('urlrouter');

connect(urlrouter(function (app) {
  app.get('/', function (req, res, next) {
    res.end('hello urlrouter');
  });
  app.get('/user/:id([0-9]+)', function (req, res, next) {
    res.end('hello user ' + req.params.id);
  });
})).listen(3000);

Several callbacks may also be passed.

function loadUser(req, res, next) {
  // You would fetch user from the db
  var user = users[req.params.id];
  if (user) {
    req.user = user;
    next();
  } else {
    next(new Error('Failed to load user ' + req.params.id));
  }
}

app.get('/user/:id', loadUser, function () {
  // ...
});

These callbacks can be passed within arrays as well.

var middleware = [loadUser, loadForum, loadThread];
app.post('/forum/:fid/thread/:tid', middleware, function () {
 // ...
});

Using with http.createServer()

var http = require('http');
var urlrouter = require('urlrouter');

var options = {
  pageNotFound: function (req, res) {
    res.statusCode = 404;
    res.end('er... some page miss...');
  },
  errorHandler: function (req, res) {
    res.statusCode = 500;
    res.end('oops..error occurred');
  }
};

function loadUser(req, res, next) {
  // You would fetch user from the db
  var user = users[req.params.id];
  if (user) {
    req.user = user;
    next();
  } else {
    next(new Error('Failed to load user ' + req.params.id));
  }
}

var routerMiddleware = urlrouter(function (app) {
  app.get('/', function (req, res) {
    res.end('GET home page' + req.url + ' , headers: ' + JSON.stringify(req.headers));
  });
  // with route middleware
  app.get('/user/:id', loadUser, function (req, res) {
    res.end('user: ' + req.params.id);
  });

  // GET /admin 301 redirect to /admin/
  app.redirect('/admin', '/admin/');

  app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, loadUser, function (req, res) {
    res.end(req.url + ' : ' + req.params);
  });

  app.get('/foo', function (req, res) {
    res.end('GET ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
  });

  app.post('/new', function (req, res) {
    res.write('POST ' + req.url + ' start...\n\n');
    var counter = 0;
    req.on('data', function (data) {
      counter++;
      res.write('data' + counter + ': ' + data.toString() + '\n\n');
    });
    req.on('end', function () {
      res.end('POST ' + req.url + ' end.\n');
    });
  });

  app.put('/update', function (req, res) {
    res.end('PUT ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
  });

  app.delete('/remove', function (req, res) {
    res.end('DELETE ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
  });

  app.options('/check', function (req, res) {
    res.end('OPTIONS ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
  });

  app.all('/all', function (req, res) {
    res.end('ALL methods request /all should be handled' + ' , headers: ' + JSON.stringify(req.headers));
  });
}, options);

http.createServer(routerMiddleware).listen(3000);

Contributors

$ git summary

 project  : urlrouter
 repo age : 1 year, 1 month
 active   : 16 days
 commits  : 38
 files    : 19
 authors  :
    33  fengmk2                 86.8%
     4  rockdai                 10.5%
     1  rock                    2.6%

License

(The MIT License)

Copyright (c) 2012 - 2013 fengmk2 [email protected].

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

urlrouter's People

Contributors

fengmk2 avatar rockdai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

urlrouter's Issues

archive this repo?

ref: expressjs/discussions#134

@fengmk2 - you have been identified as the last / most active committer in this repo which is inactive for a while. This ping is to check with you to determine:

  • whether do you have any specific maintenance plans with this repo
  • are you fine for this repo to be archived
  • do you believe if there is another stake holder / affected party who should know

thanks!

docs for formatting

It's looks like it's similar to regex, but with some custom name-group type stuff. It's definitely not the router from connect 1.x (which I maintained for a little while after its abandonment, btw https://github.com/coolaj86/connect_router).

The link provided in "express format routing" is broken, so there aren't any docs there.

old router vs new router

In the olden days I could use a route such as

/files/:id/:filename? and that would match paths like

  • /files/1234
  • /files/1234/big
  • /files/1234/big.png
  • /files/1234/big-bird
  • /files/1234/big-bird.png

I just converted an old-old-old app over and this new urlrouter handles that for paths like

  • /files/1234
  • /files/1234/big
  • /files/1234-678/big

But not

  • /files/1234-678/big.png
  • /files/1234-678/big-bird
  • /files/1234-678/big-bird.png

Is that a bug? It seems like it since :id is matching as expected, so why is :filename matching differently, y'know?

If not, is there an equivalent?

As a quick and simple fix I expanded to matching

  • /files/:id/:filename
  • /files/:id

and that seems to work as expected

Anyway, just wanted to know if "that's how we roll" these days or if it's a bug.

undefined is not a function

when set route function is undefined, will throw an error.

TypeError: undefined is not a function
    at Object.lookup [as handle] (/Users/mk2/git/todo/node_modules/urlrouter/lib/urlrouter.js:71:18)
    at next (/Users/mk2/git/todo/node_modules/connect/lib/proto.js:190:15)
    at Object.handle (/Users/mk2/git/todo/node_modules/connect-render/lib/render.js:173:5)
    at next (/Users/mk2/git/todo/node_modules/connect/lib/proto.js:190:15)
    at Object.handle (/Users/mk2/git/todo/node_modules/connect/lib/middleware/csrf.js:48:88)
    at next (/Users/mk2/git/todo/node_modules/connect/lib/proto.js:190:15)
    at /Users/mk2/git/todo/node_modules/connect/lib/middleware/session.js:299:9
    at /Users/mk2/git/todo/node_modules/connect/lib/middleware/session.js:322:9
    at Array.0 (/Users/mk2/git/todo/node_modules/connect/lib/middleware/session/memory.js:52:9)
    at EventEmitter._tickCallback (node.js:192:40)

Routing .all()

How would you feel about a .all() that matches any verb?

Using with connect.static()

When I combine connect.static() and urlrouter static paths return 404 not found error.

node v0.10.13
urlrouter v0.0.5
connect v.2.12.0

please ignore this issue

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.