we are creating a separate API app, but forced to make it part of the existing express.js main app.
My question is how to put API authentication in a proper place. I want to make it a middleware, and behind which would be the app.routes middleware to handle the API routes.
Is it possible to either
or
edit:
also, suppose I go with the second option, is it possible to expose the two middlewares (auth + api app.routes) as a single middleware in the global list?
You can route to an express app, with it's own middleware. So one app for most routes, and an api app for api routes.
example: http://runnable.com/UWx2MZS8trEHAACZ
var express = require('express');
var app = express();
var api = express();
app.configure(function () {
app.use(express.static(__dirname));
});
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/api*', api);
api.configure(function () {
api.use(function (req, res, next) {
process.stdout.write('checking');
setTimeout(process.stdout.write.bind(process.stdout, '.'), 100);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 200);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 300);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 400);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 500);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 600);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 700);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 800);
setTimeout(process.stdout.write.bind(process.stdout, '.\n'), 900);
setTimeout(function () {
next();
}, 1000);
});
});
api.get('*', function (req, res) {
res.send('Hello API');
});
app.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);