Skip to content

Commit

Permalink
Merge f175698 into 41f61cb
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 7, 2022
2 parents 41f61cb + f175698 commit 9a4c3a9
Show file tree
Hide file tree
Showing 17 changed files with 743 additions and 411 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
@@ -1,5 +1,6 @@
root: true
extends:
- standard
- plugin:markdown/recommended
plugins:
- markdown
Expand Down
16 changes: 0 additions & 16 deletions .github/workflows/ci.yml
Expand Up @@ -10,7 +10,6 @@ jobs:
strategy:
matrix:
name:
- Node.js 0.8
- Node.js 0.10
- Node.js 0.12
- io.js 1.x
Expand All @@ -33,11 +32,6 @@ jobs:
- Node.js 18.x

include:
- name: Node.js 0.8
node-version: "0.8"
npm-i: mocha@2.5.3 supertest@1.1.0
npm-rm: nyc

- name: Node.js 0.10
node-version: "0.10"
npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0
Expand Down Expand Up @@ -118,21 +112,11 @@ jobs:
shell: bash -eo pipefail -l {0}
run: |
nvm install --default ${{ matrix.node-version }}
if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then
nvm install --alias=npm 0.10
nvm use ${{ matrix.node-version }}
sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")"
npm config set strict-ssl false
fi
dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH"
- name: Configure npm
run: npm config set shrinkwrap false

- name: Remove npm module(s) ${{ matrix.npm-rm }}
run: npm rm --silent --save-dev ${{ matrix.npm-rm }}
if: matrix.npm-rm != ''

- name: Install npm module(s) ${{ matrix.npm-i }}
run: npm install --save-dev ${{ matrix.npm-i }}
if: matrix.npm-i != ''
Expand Down
42 changes: 42 additions & 0 deletions HISTORY.md
@@ -1,3 +1,45 @@
2.x
===

This incorporates all changes after 1.3.5 up to 1.3.7.

* Add support for returned, rejected Promises to `router.param`

2.0.0-beta.1 / 2020-03-29
=========================

This incorporates all changes after 1.3.3 up to 1.3.5.

* Internalize private `router.process_params` method
* Remove `debug` dependency
* deps: array-flatten@3.0.0
* deps: parseurl@~1.3.3
* deps: path-to-regexp@3.2.0
- Add new `?`, `*`, and `+` parameter modifiers
- Matching group expressions are only RegExp syntax.
`(*)` is no longer valid and must be written as `(.*)`, for example.
- Named matching groups no longer available by position in `req.params`.
`/:foo(.*)` only captures as `req.params.foo` and not available as
`req.params[0]`.
- Regular expressions can only be used in a matching group.
`/\\d+` is no longer valid and must be written as `/(\\d+)`.
- Special `*` path segment behavior removed.
`/foo/*/bar` will match a literal `*` as the middle segment.
* deps: setprototypeof@1.2.0

2.0.0-alpha.1 / 2018-07-27
==========================

* Add basic support for returned, rejected Promises
- Rejected Promises from middleware functions `next(error)`
* Drop support for Node.js below 0.10
* deps: debug@3.1.0
- Add `DEBUG_HIDE_DATE` environment variable
- Change timer to per-namespace instead of global
- Change non-TTY date format
- Remove `DEBUG_FD` environment variable support
- Support 256 namespace colors

1.3.7 / 2022-04-28
==================

Expand Down
55 changes: 39 additions & 16 deletions README.md
Expand Up @@ -22,16 +22,16 @@ $ npm install router

```js
var finalhandler = require('finalhandler')
var http = require('http')
var Router = require('router')
var http = require('http')
var Router = require('router')

var router = Router()
router.get('/', function (req, res) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('Hello World!')
})

var server = http.createServer(function(req, res) {
var server = http.createServer(function (req, res) {
router(req, res, finalhandler(req, res))
})

Expand Down Expand Up @@ -66,6 +66,8 @@ consider it one for handling `OPTIONS` requests.
* Note: If a `path` is specified, that `path` is stripped from the start of
`req.url`.

<!-- eslint-disable no-undef -->

```js
router.use(function (req, res, next) {
// do your things
Expand All @@ -89,6 +91,8 @@ the routing functionality in `router`.
Method middleware and handlers follow usual [middleware](#middleware) behavior,
except they will only be called when the method and path match the request.

<!-- eslint-disable no-undef -->

```js
// handle a `GET` request
router.get('/', function (req, res) {
Expand All @@ -111,11 +115,18 @@ Maps the specified path parameter `name` to a specialized param-capturing middle

This function positions the middleware in the same stack as `.use`.

The function can optionally return a `Promise` object. If a `Promise` object
is returned from the function, the router will attach an `onRejected` callback
using `.then`. If the promise is rejected, `next` will be called with the
rejected value, or an error if the value is falsy.

Parameter mapping is used to provide pre-conditions to routes
which use normalized placeholders. For example a _:user_id_ parameter
could automatically load a user's information from the database without
any additional code:

<!-- eslint-disable no-undef -->

```js
router.param('user_id', function (req, res, next, id) {
User.find(id, function (err, user) {
Expand All @@ -142,6 +153,8 @@ Routes can be used to handle http `methods` with their own, optional middleware.
Using `router.route(path)` is a recommended approach to avoiding duplicate
route naming and thus typo errors.

<!-- eslint-disable no-undef, no-unused-vars -->

```js
var api = router.route('/api/')
```
Expand All @@ -156,6 +169,8 @@ Represents a single route as an instance that can be used to handle http
These are functions which you can directly call on a route to register a new
`handler` for the `method` on the route.

<!-- eslint-disable no-undef -->

```js
// handle a `GET` request
var status = router.route('/status')
Expand All @@ -173,12 +188,14 @@ Adds a handler for all HTTP methods to this route.
The handler can behave like middleware and call `next` to continue processing
rather than responding.

<!-- eslint-disable no-undef -->

```js
router.route('/')
.all(function (req, res, next) {
next()
})
.all(check_something)
.all(checkSomething)
.get(function (req, res) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('Hello World!')
Expand All @@ -195,20 +212,27 @@ format is with three parameters - "req", "res" and "next".
- `res` - This is a [HTTP server response](https://nodejs.org/api/http.html#http_class_http_serverresponse) instance.
- `next` - Calling this function that tells `router` to proceed to the next matching middleware or method handler. It accepts an error as the first argument.

The function can optionally return a `Promise` object. If a `Promise` object
is returned from the function, the router will attach an `onRejected` callback
using `.then`. If the promise is rejected, `next` will be called with the
rejected value, or an error if the value is falsy.

Middleware and method handlers can also be defined with four arguments. When
the function has four parameters defined, the first argument is an error and
subsequent arguments remain, becoming - "err", "req", "res", "next". These
functions are "error handling middleware", and can be used for handling
errors that occurred in previous handlers (E.g. from calling `next(err)`).
This is most used when you want to define arbitrary rendering of errors.

<!-- eslint-disable no-undef -->

```js
router.get('/error_route', function (req, res, next) {
return next(new Error('Bad Request'))
})

router.use(function (err, req, res, next) {
res.end(err.message) //=> "Bad Request"
res.end(err.message) //= > "Bad Request"
})
```

Expand All @@ -220,18 +244,18 @@ bypassed - only error handling middleware will be invoked with an error.

```js
// import our modules
var http = require('http')
var Router = require('router')
var http = require('http')
var Router = require('router')
var finalhandler = require('finalhandler')
var compression = require('compression')
var bodyParser = require('body-parser')
var compression = require('compression')
var bodyParser = require('body-parser')

// store our message to display
var message = "Hello World!"
var message = 'Hello World!'

// initialize the router & server and add a final callback.
var router = Router()
var server = http.createServer(function onRequest(req, res) {
var server = http.createServer(function onRequest (req, res) {
router(req, res, finalhandler(req, res))
})

Expand Down Expand Up @@ -285,17 +309,16 @@ curl http://127.0.0.1:8080/api/set-message -X PATCH -H "Content-Type: applicatio
### Example using mergeParams

```js
var http = require('http')
var Router = require('router')
var http = require('http')
var Router = require('router')
var finalhandler = require('finalhandler')

// this example is about the mergeParams option
var opts = { mergeParams: true }

// make a router with out special options
var router = Router(opts)
var server = http.createServer(function onRequest(req, res) {

var server = http.createServer(function onRequest (req, res) {
// set something to be passed into the router
req.params = { type: 'kitten' }

Expand Down Expand Up @@ -351,7 +374,7 @@ var Router = require('router')

// create the router and server
var router = new Router()
var server = http.createServer(function onRequest(req, res) {
var server = http.createServer(function onRequest (req, res) {
router(req, res, finalhandler(req, res))
})

Expand Down

0 comments on commit 9a4c3a9

Please sign in to comment.