Permalink
Browse files

Moving middleware to a route so it is restricted to an endpoint

  • Loading branch information...
1 parent dbb54c5 commit d54ba62bdbf6841cd708bb1f351f10d204a2ce20 @kulor committed Sep 2, 2012
Showing with 23 additions and 23 deletions.
  1. +22 −0 server/node-express/lib/upload-routes.js
  2. +1 −23 server/node-express/server.js
@@ -118,6 +118,28 @@ var configure = function(options){
head: function(req, res){
setNoCacheHeaders(res);
res.end();
+ },
+
+ options: function(req, res, next){
+ if (req.method === 'OPTIONS') {
+ res.statusCode = 204;
+ res.setHeader('Allow', '');
+ res.end();
+ } else {
+ next();
+ }
+ },
+
+ accessControl: function(req, res, next){
+ res.setHeader(
+ 'Access-Control-Allow-Origin',
+ options.accessControl.allowOrigin
+ );
+ res.setHeader(
+ 'Access-Control-Allow-Methods',
+ options.accessControl.allowMethods
+ );
+ next();
}
}
}
@@ -48,29 +48,7 @@ app.configure(function(){
var uploadRoutes = require('./lib/upload-routes').configure(app.get('options'));
-app.use(function(req, res, next){
- var options = app.get('options');
- res.setHeader(
- 'Access-Control-Allow-Origin',
- options.accessControl.allowOrigin
- );
- res.setHeader(
- 'Access-Control-Allow-Methods',
- options.accessControl.allowMethods
- );
- next();
-});
-
-app.use(function(req, res, next){
- if (req.method === 'OPTIONS') {
- res.statusCode = 204;
- res.setHeader('Allow', '');
- res.end();
- } else {
- next();
- }
-});
-
+app.all('/', uploadRoutes.accessControl, uploadRoutes.options);
app.get('/', uploadRoutes.get);
app.post('/', uploadRoutes.post);
app.delete('/', uploadRoutes.delete);

0 comments on commit d54ba62

Please sign in to comment.