📜 ⬆️ ⬇️

Connect 2 - New additions, improvements and documentation!



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) { // render a client-error page } else { // render a server-error page } }); 


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


2.0.0 / 2011-10-05


Now we are waiting for express 3!

Sources and references:
Post from TJ Holowaychuk blog
Connect page
Github connect

Source: https://habr.com/ru/post/139203/


All Articles