@@ -71,83 +71,85 @@ care to check if the request has timedout before you continue to act
7171on the request.
7272
7373``` javascript
74- var express = require (' express' );
75- var timeout = require (' connect-timeout' );
74+ var bodyParser = require (' body-parser' )
75+ var cookieParser = require (' cookie-parser' )
76+ var express = require (' express' )
77+ var timeout = require (' connect-timeout' )
7678
7779// example of using this top-level; note the use of haltOnTimedout
7880// after every middleware; it will stop the request flow on a timeout
79- var app = express ();
80- app .use (timeout (' 5s' ));
81- app .use (bodyParser ());
82- app .use (haltOnTimedout);
83- app .use (cookieParser ());
84- app .use (haltOnTimedout);
81+ var app = express ()
82+ app .use (timeout (' 5s' ))
83+ app .use (bodyParser ())
84+ app .use (haltOnTimedout)
85+ app .use (cookieParser ())
86+ app .use (haltOnTimedout)
8587
8688// Add your routes here, etc.
8789
88- function haltOnTimedout (req , res , next ){
89- if (! req .timedout ) next ();
90+ function haltOnTimedout (req , res , next ) {
91+ if (! req .timedout ) next ()
9092}
9193
92- app .listen (3000 );
94+ app .listen (3000 )
9395```
9496
9597### express 3.x
9698
9799``` javascript
98- var express = require (' express' );
99- var bodyParser = require (' body-parser' );
100- var timeout = require (' connect-timeout' );
101-
102- var app = express ();
103- app .post (' /save' , timeout (' 5s' ), bodyParser .json (), haltOnTimedout, function (req , res , next ){
104- savePost (req .body , function (err , id ){
105- if (err) return next (err);
106- if (req .timedout ) return ;
107- res .send (' saved as id ' + id);
108- });
109- });
110-
111- function haltOnTimedout (req , res , next ){
112- if (! req .timedout ) next ();
100+ var express = require (' express' )
101+ var bodyParser = require (' body-parser' )
102+ var timeout = require (' connect-timeout' )
103+
104+ var app = express ()
105+ app .post (' /save' , timeout (' 5s' ), bodyParser .json (), haltOnTimedout, function (req , res , next ) {
106+ savePost (req .body , function (err , id ) {
107+ if (err) return next (err)
108+ if (req .timedout ) return
109+ res .send (' saved as id ' + id)
110+ })
111+ })
112+
113+ function haltOnTimedout (req , res , next ) {
114+ if (! req .timedout ) next ()
113115}
114116
115- function savePost (post , cb ){
116- setTimeout (function () {
117- cb (null , ((Math .random ()* 40000 ) >>> 0 ));
118- }, (Math .random ()* 7000 ) >>> 0 );
117+ function savePost (post , cb ) {
118+ setTimeout (function () {
119+ cb (null , ((Math .random () * 40000 ) >>> 0 ))
120+ }, (Math .random () * 7000 ) >>> 0 )
119121}
120122
121- app .listen (3000 );
123+ app .listen (3000 )
122124```
123125
124126### connect
125127
126128``` javascript
127- var bodyParser = require (' body-parser' );
128- var connect = require (' connect' );
129- var timeout = require (' connect-timeout' );
130-
131- var app = connect ();
132- app .use (' /save' , timeout (' 5s' ), bodyParser .json (), haltOnTimedout, function (req , res , next ){
133- savePost (req .body , function (err , id ){
134- if (err) return next (err);
135- if (req .timedout ) return ;
136- res .send (' saved as id ' + id);
137- });
138- });
139-
140- function haltOnTimedout (req , res , next ){
141- if (! req .timedout ) next ();
129+ var bodyParser = require (' body-parser' )
130+ var connect = require (' connect' )
131+ var timeout = require (' connect-timeout' )
132+
133+ var app = connect ()
134+ app .use (' /save' , timeout (' 5s' ), bodyParser .json (), haltOnTimedout, function (req , res , next ) {
135+ savePost (req .body , function (err , id ) {
136+ if (err) return next (err)
137+ if (req .timedout ) return
138+ res .send (' saved as id ' + id)
139+ })
140+ })
141+
142+ function haltOnTimedout (req , res , next ) {
143+ if (! req .timedout ) next ()
142144}
143145
144- function savePost (post , cb ){
145- setTimeout (function () {
146- cb (null , ((Math .random ()* 40000 ) >>> 0 ));
147- }, (Math .random ()* 7000 ) >>> 0 );
146+ function savePost (post , cb ) {
147+ setTimeout (function () {
148+ cb (null , ((Math .random () * 40000 ) >>> 0 ))
149+ }, (Math .random () * 7000 ) >>> 0 )
148150}
149151
150- app .listen (3000 );
152+ app .listen (3000 )
151153```
152154
153155## License
0 commit comments