Skip to content

Commit

Permalink
Fix serve static files with query in URL
Browse files Browse the repository at this point in the history
PR-URL: #338
  • Loading branch information
tshemsedinov committed Jul 26, 2022
1 parent 8c07290 commit 4b360a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Add stream types
- Fix browser client WebsocketTransport open
- Add method for a possibility to delete session token from the application routes
- Fix serve static files with query in URL

## [3.0.0-alpha.2][] - 2022-07-07

Expand Down
8 changes: 6 additions & 2 deletions lib/static.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
'use strict';

const path = require('path');
const metautil = require('metautil');

const index = (url) => path.join(url, 'index.html');

const serveStatic = (channel) => {
const { req, res, application } = channel;
if (res.writableEnded) return;
const { url } = req;
const filePath = url.endsWith('/') ? index(url) : url;
const [urlPath, params] = metautil.split(url, '?');
const filePath = urlPath.endsWith('/') ? index(urlPath) : urlPath;
const fileExt = path.extname(filePath).substring(1);
console.log({ url, urlPath, params, filePath, fileExt });
const data = application.getStaticFile(filePath);
if (data) {
channel.write(data, 200, fileExt);
return;
}
if (fileExt !== 'html') {
if (application.getStaticFile(index(filePath))) {
channel.redirect(filePath + '/');
const query = params ? '?' + params : '';
channel.redirect(filePath + '/' + query);
return;
}
}
Expand Down

0 comments on commit 4b360a9

Please sign in to comment.