Skip to content

Commit 922251a

Browse files
LinusUdavimacedo
authored andcommitted
Avoid calling allowCrossDomain twice per request (#5682)
`api.use('/', middleware, ...)` will end up calling `middleware` for _every_ request, even if no routers in the `...` part matches. This is because passing a router to express is just like passing any other route handler. The only thing that happens when it doesn't match a route is that it calls `next`, but by that point, the middleware has already run. The changes in the PR avoids adding the middleware twice for every route except file upload routes. Which will make express not call `allowCrossDomain` twice for every incoming request.
1 parent 559096f commit 922251a

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

src/ParseServer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ class ParseServer {
151151
// It's the equivalent of https://api.parse.com/1 in the hosted Parse API.
152152
var api = express();
153153
//api.use("/apps", express.static(__dirname + "/public"));
154+
api.use(middlewares.allowCrossDomain);
154155
// File handling needs to be before default middlewares are applied
155156
api.use(
156157
'/',
157-
middlewares.allowCrossDomain,
158158
new FilesRouter().expressRouter({
159159
maxUploadSize: maxUploadSize,
160160
})
@@ -173,7 +173,6 @@ class ParseServer {
173173
);
174174

175175
api.use(bodyParser.json({ type: '*/*', limit: maxUploadSize }));
176-
api.use(middlewares.allowCrossDomain);
177176
api.use(middlewares.allowMethodOverride);
178177
api.use(middlewares.handleParseHeaders);
179178

0 commit comments

Comments
 (0)