Skip to content

Commit

Permalink
added static asset gestion
Browse files Browse the repository at this point in the history
  • Loading branch information
andy.crepin committed Mar 18, 2019
1 parent e55a304 commit 4026cdf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-dependency-injection",
"version": "1.4.5",
"version": "1.5.0",
"description": "a dependency injector for express using decorators",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions src/schema/router/static.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class StaticSchema {

public path?: string;
public dir: string;
}
33 changes: 33 additions & 0 deletions src/server/server.abstract.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { describe } from "mocha";
import { AbstractServer } from "./server.abstract";
import { ExRouter } from "../decorators/router.decorator";
import { ExServer } from "../decorators/server.decorator";
import * as chai from "chai";
import * as spies from "chai-spies";
import { expect } from "chai";
import { inspect } from "util";
import { AbstractRouter } from "../router/router.abstract";

chai.use(spies);

Expand Down Expand Up @@ -32,4 +36,33 @@ describe('AbstractServer', () => {
test.listen(3000, spyStub);
expect(spyStub).to.have.been.called.once;
})

it('should register static ressources with and without virtual path', () => {

@ExRouter({
path: '/'
})
class EmptyRouter extends AbstractRouter {


}

@ExServer({
main: EmptyRouter
})
class App extends AbstractServer {

}
const fakeStatics = [{
path: '/main',
dir: './fake1'
},{
path: '/main',
dir: './fake2'
}],
test = new App();
test.addStatics(fakeStatics);
// two static routes registered, plus 3 natives, so it should be 5
expect(Reflect.get(test, 'app')._router.stack.length).equal(5);
})
})
11 changes: 11 additions & 0 deletions src/server/server.abstract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { Express } from 'express';
import { StaticSchema } from '../schema/router/static.schema';
import * as express from "express"

export abstract class AbstractServer {

protected app: Express;
protected statics : Array<StaticSchema>;

public addStatics(args: Array<StaticSchema>) {

args.forEach(staticSchema => staticSchema.path ?
this.app.use(staticSchema.path, express.static(staticSchema.dir)) :
this.app.use(express.static(staticSchema.dir))
);
}

public listen(port: number, callback?: Function) {

Expand Down

0 comments on commit 4026cdf

Please sign in to comment.