I have an API that can be called either through a browser, where the requests are transactional, or directly connected to the session, for example. using curl where the queries are atomic. Browser requests must first authenticate and then use an express session (connect.sid) for subsequent authorization, direct API calls use the header: Authorization: "SOMETOKEN" , which must be sent for each request.
The problem is that since I use the same web server to serve both atomic and transactional traffic, every API call gets an Express session uselessly. Each response includes a Set-Cookie, and all of these sessions populate my session store. Therefore: how can I prevent Express from entering a new sess key in the storage (Redis) when the request contains an authorization header?
Note. I understand that a more classic approach would be to have a separate API server and a separate WEB server, but why not run them on the same machine? For me, the difference is that the API serves the data, and the WEB serves the views, but in addition, they are part of the same application. Itβs just that Iβll also allow users to directly access their data and not force them to use my interface.
Express configuration
module.exports = function(app, exp, sessionStore, cookieParser, passport, flash) { app.configure(function(){
Route Example
app.get('/status/past_week', MID.ensureAuthenticated, MID.markStart, function(req, res) { WEB.getStatus('week', function(err, statuses){ if ( err ) res.send(500, err); else res.send(200, statuses); }); });
Middleware Authorization
MID.ensureAuthenticated = function(req, res, next) { if ( req.isAuthenticated() ) return next(); else { isAuthorised(req, function(err, authorised){ if ( err ) return res.redirect('/'); else if ( authorised ) return next(); else return res.redirect('/'); }); } function isAuthorised(req, callback){ var authHeader = req.headers.authorization; if ( authHeader ) {