
Not so long ago, namely 2011-10-05, a new version of connect 2.0 framework has been released. On Habré, a space was noticed, and then there were a couple of free hours. I will write immediately about version 2.0.1 released on February 29th. In the second version made a lot of very necessary and long-awaited changes.
HTTP and HTTPS for one server application!
Previously connect.Server was inherited from net.Server from the NodeJS core, this caused inconvenience for creating simultaneously HTTP and HTTPS applications. As a result, connect () (previously connect.createServer ()) has become a new feature more universal. This means that you can skip the app.listen () call, and simply use net.Server from NodeJS in an example:
var connect = require('connect'), http = require('http'), https = require('https'); tlsOptions = { key: fs.readFileSync('keys/key.pem'), cert: fs.readFileSync('keys/cert.pem') }; var app = connect() .use(function(req, res) { res.end('hello habrahabr\n'); }) http.createServer(app).listen(80); https.createServer(tlsOptions, app).listen(443);
')
Improved query parser
In addition, bodyParser () added new functions for processing incoming requests: json () for application / json requests, multipart () for multipart / form-data requests, and urlencoded () for application / x-www-form-urlencoded requests. All of them represent new access to the transmitted data in the queries for req.body, and the multipart () function provides access to the passed parameters not only in req.body, but also req.files to the downloaded files.
You can connect passers separately:
app.use(connect.json()); app.use(connect.urlencoded()); app.use(connect.multipart());
this will be equivalent to writing:
app.use(connect.bodyParser());
Kernel-level deflate and gzip support
Since Node 0.6.0, the native use of compression (zlib.createGzip and zlib.createDeflate) became available, so the add-in compress () was added to connect to support them. The addon has
compression settings passed to native functions.
Cookie parser
Signatures are now available to add cookieParser (). Previously, you had to use session ({secret: string}) and the addition session () itself. Now you can work with signed cookies and your sessions through the req.signedCookies property, as well as to work with those not signed through the req.cookies property.
connect() .use(connect.cookieParser('keyboard cat')) .use(function(req, res, next){ res.end(JSON.stringify(req.cookies)); })
Error processing
Earlier work with errors was a problem. All errors were intercepted by errorHandler () in connect () itself and were issued by standard pages. Now it is possible to override the error handler, while also using next (err) in your code.
app.use(function(err, req, res, next){ if (4 == err.status / 100) {
Sessions
As mentioned session () no longer require a secret. For cookies, the .maxAge property was null by default, which meant that all the cookies that were set were deleted when the browser was closed.
Side supplement
All additions to connect () must remain operable. This is all not compatible with express 2.0, wait for express 3.0 to be released!
List of all changes
Were made significant changes described, as well as several others that do not affect the API. All tests were performed using Mocha.
2.0.1 / 2012-02-29- Added test coverage vhost () add-ons
- Changed cookieParser () signature for cookie support for using SHA-2 [senotrusov]
- Fixed static () Range: response from 416 if not feasible
- Fixed vhost () addition. Closing # 494
2.0.0 / 2011-10-05- Added cookieSession () add-on for cookie sessions only
- Added compress () add-on for gzip / deflate support
- Added session () “proxy” setting for checking X-Forwarded-Proto
- Added json () add-on to support “application / json”
- Added urlencoded () add-on to support “application / x-www-form-urlencoded”
- Added multipart () add-on to support “multipart / form-data”
- Added cookieParser (secret) access to subscribed cookies
- Added support for signing a cookie in cookieParser ()
- Added support for JSON serialization of cookies in cookieParser ()
- Added err.status support in Connect by default
- Added X-Cache MISS / HIT to staticCache ()
- Added public res.headerSent checking nodes res._headerSent until node does
- Changed basicAuth () req.remoteUser to req.user
- Changed the default session () in the cookie session cookie. Closes # 475
- Changed no longer in lowercase names in the cookie
- Changed bodyParser () now uses json (), urlencoded () and multipart ()
- Modified errorHandler () override capability (in development)
- Changed using next () to intercept errors gives more possibilities for their further processing.
- Removed http [s] .Server inheritance, to be able to create http and https in one application
- Removed .createServer () (use connect ())
- Removed the secret property in session (), use cookieParser (secret)
- Removed connect.session.ignore array support
- Removed router () addition. Closes # 262
- Fixed set-cookie only 1 time per session
- Fixed FQDN support. Does not add "/"
- Fixed 404 when performing XSS. Closes # 473
- Fixed HEAD support for 404 and 500 generated by Connect
Now we are waiting for express 3!
Sources and references:
Post from TJ Holowaychuk blogConnect pageGithub connect