aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/connect-timeout/README.md
blob: e1a899afcfba0c66efbf5416f4a4ec3cec00e865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# connect-timeout

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Gratipay][gratipay-image]][gratipay-url]

Times out the request in `ms`, defaulting to `5000`.

## Install

```sh
$ npm install connect-timeout
```

## API

**NOTE** This module is not recommend as a "top-level" middleware (i.e.
`app.use(timeout('5s'))`) unless you take precautions to halt your own
middleware processing. See [as top-level middleware](#as-top-level-middleware)
for how to use as a top-level middleware.

### timeout(time, [options])

Returns middleware that times out in `time` milliseconds. `time` can also
be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
module. On timeout, `req` will emit `"timeout"`.

#### Options

The `timeout` function takes an optional `options` object that may contain
any of the following keys:

##### respond

Controls if this module will "respond" in the form of forwarding an error.
If `true`, the timeout error is passed to `next()` so that you may customize
the response behavior. This error has a `.timeout` property as well as
`.status == 503`. This defaults to `true`.

### req.clearTimeout()

Clears the timeout on the request. The timeout is completely removed and
will not fire for this request in the future.

### req.timedout

`true` if timeout fired; `false` otherwise.

## Examples

### as top-level middleware

Because of the way middleware processing works, this once this module
passes the request to the next middleware (which it has to do in order
for you to do work), it can no longer stop the flow, so you must take
care to check if the request has timedout before you continue to act
on the request.

```javascript
var express = require('express');
var timeout = require('connect-timeout');

// example of using this top-level; note the use of haltOnTimedout
// after every middleware; it will stop the request flow on a timeout
var app = express();
app.use(timeout('5s'));
app.use(bodyParser());
app.use(haltOnTimedout);
app.use(cookieParser());
app.use(haltOnTimedout);

// Add your routes here, etc.

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

app.listen(3000);
```

### express 3.x

```javascript
var express = require('express');
var bodyParser = require('body-parser');
var timeout = require('connect-timeout');

var app = express();
app.post('/save', timeout('5s'), bodyParser.json(), haltOnTimedout, function(req, res, next){
  savePost(req.body, function(err, id){
    if (err) return next(err);
    if (req.timedout) return;
    res.send('saved as id ' + id);
  });
});

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

function savePost(post, cb){
  setTimeout(function(){
    cb(null, ((Math.random()* 40000) >>> 0));
  }, (Math.random()* 7000) >>> 0));
}

app.listen(3000);
```

### connect

```javascript
var bodyParser = require('body-parser');
var connect = require('connect');
var timeout = require('connect-timeout');

var app = require('connect');
app.use('/save', timeout('5s'), bodyParser.json(), haltOnTimedout, function(req, res, next){
  savePost(req.body, function(err, id){
    if (err) return next(err);
    if (req.timedout) return;
    res.send('saved as id ' + id);
  });
});

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

function savePost(post, cb){
  setTimeout(function(){
    cb(null, ((Math.random()* 40000) >>> 0));
  }, (Math.random()* 7000) >>> 0));
}

app.listen(3000);
```

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/connect-timeout.svg
[npm-url]: https://npmjs.org/package/connect-timeout
[travis-image]: https://img.shields.io/travis/expressjs/timeout/master.svg
[travis-url]: https://travis-ci.org/expressjs/timeout
[coveralls-image]: https://img.shields.io/coveralls/expressjs/timeout/master.svg
[coveralls-url]: https://coveralls.io/r/expressjs/timeout?branch=master
[downloads-image]: https://img.shields.io/npm/dm/connect-timeout.svg
[downloads-url]: https://npmjs.org/package/connect-timeout
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
[gratipay-url]: https://www.gratipay.com/dougwilson/