Skip to content

Commit 5a92082

Browse files
committed
Updated to retrieve files with long paths with a certain extension using addAssetPath and added tests to cover the case
1 parent 3f088b1 commit 5a92082

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ module.exports = (http) => {
8585
let urlParts = url.split('/');
8686

8787
if(url.includes('.')) { // If the url is for an asset
88-
let fileType = url.split('.')[2] || url.split('.')[1];
88+
let fileType = url.split('.');
89+
fileType = fileType[fileType.length - 1];
8990
let file = url.replace('/', '');
9091
let assetType = urlParts[urlParts.length - 2];
9192

@@ -100,7 +101,10 @@ module.exports = (http) => {
100101
}
101102
else {
102103
let path = assetType;
103-
file = this.assetPaths[file].replace(`${path}/`, '');
104+
105+
if(this.assetPaths.hasOwnProperty(fileType)) {
106+
file = this.assetPaths[file].replace(`${path}/`, '');
107+
}
104108

105109
this.renderAsset(path, file, path, res);
106110
}
@@ -184,7 +188,7 @@ module.exports = (http) => {
184188
* @param {Server.Response} res
185189
*/
186190
renderAsset(path, file, assetType, res) {
187-
fs.readFile(`${path}/${file}`, (err, data) => {
191+
fs.readFile(`${path}${file}`, (err, data) => {
188192
if(err) {
189193
res.statusCode = 404;
190194
res.end(`File ${path}/${file} doesn't exist`);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "small-router",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Small router module that creates http server and runs callbacks that have have been added for a route via the api",
55
"main": "index.js",
66
"scripts": {

tests/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('server', () => {
4040
});
4141

4242
router.addAssetPath('images', 'tests/images/');
43+
router.addAssetPath('js', 'tests/js/');
4344

4445
router.listen(8000);
4546
});
@@ -170,7 +171,7 @@ describe('server', () => {
170171
});
171172
});
172173

173-
describe('/images/loader.gif', (parameters) => {
174+
describe('/images/loader.gif', () => {
174175
let route = 'images/loader.gif';
175176

176177
it('it should return 200 status code', (done) => {
@@ -197,6 +198,33 @@ describe('server', () => {
197198
});
198199
});
199200

201+
describe('/jspm_packages/github/capaj/systemjs-hot-reloader@0.5.7.js', () => {
202+
let route = '/jspm_packages/github/capaj/systemjs-hot-reloader@0.5.7.js';
203+
204+
it('it should return 200 status code', (done) => {
205+
http.get(`${SERVER_URL}/${route}`, (res) => {
206+
res.statusCode.should.equal(200);
207+
done();
208+
});
209+
});
210+
211+
it('it should return contents of systemjs-hot-reloader@0.5.7.js', (done) => {
212+
http.get(`${SERVER_URL}/${route}`, (res) => {
213+
let data = '';
214+
res.on('data', chunk => data += chunk).on('end', () => {
215+
fs.readFile(`tests/js${route}`, (err, data) => {
216+
if(err) {
217+
throw err;
218+
}
219+
220+
data.should.equal(data);
221+
done();
222+
});
223+
});
224+
});
225+
});
226+
});
227+
200228
describe('/:test([a-z]+)', () => {
201229
let route = '/test-route';
202230

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "github:capaj/systemjs-hot-reloader@0.5.7/hot-reloader.js";
2+
export {default} from "github:capaj/systemjs-hot-reloader@0.5.7/hot-reloader.js";

0 commit comments

Comments
 (0)