Skip to content

Commit

Permalink
Merge pull request #1 from trinketapp/proxy-after-local-files
Browse files Browse the repository at this point in the history
move to proxy behind the static files, so you serve static files befo…
  • Loading branch information
albertjan authored Mar 7, 2017
2 parents 53c2c52 + a440301 commit 9a9492f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ Server.prototype = {

this.injectMiddleware(app);

this.configureProxy(app);

app.get('/', function(req, res) {
res.redirect('/' + String(Math.floor(Math.random() * 10000)));
});
Expand All @@ -122,6 +120,8 @@ Server.prototype = {
app.all(/^\/(?:-?[0-9]+)(\/.+)$/, serveStaticFile);
app.all(/^(.+)$/, serveStaticFile);

this.configureProxy(app);

app.use(function(err, req, res, next) {
if (err) {
log.error(err.message);
Expand All @@ -135,8 +135,8 @@ Server.prototype = {
}
});

function serveStaticFile(req, res) {
self.serveStaticFile(req.params[0], req, res);
function serveStaticFile(req, res, next) {
self.serveStaticFile(req.params[0], req, res, next);
}
},
configureExpress: function(app) {
Expand Down Expand Up @@ -320,7 +320,7 @@ Server.prototype = {
uri: bestMatch || uri.substring(1)
};
},
serveStaticFile: function(uri, req, res) {
serveStaticFile: function(uri, req, res, next) {
var self = this;
var config = this.config;
var routeRes = this.route(uri);
Expand All @@ -344,6 +344,9 @@ Server.prototype = {
fs.stat(filePath, function(err, stat) {
self.emit('file-requested', filePath);
if (err) {
if (next) {
return next('route');
}
return res.sendFile(filePath);
}
if (stat.isDirectory()) {
Expand Down
2 changes: 1 addition & 1 deletion tests/server_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('Server', function() {
}
};
request.get(options, function(err, req, text) {
expect(text).to.equal('Not found: /api3/test');
expect(text).to.equal('<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /api3/test</pre>\n</body>\n');
done();
});
});
Expand Down

0 comments on commit 9a9492f

Please sign in to comment.