forked from wbhob/nest-middlewares
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serve-static): add serve-static middleware
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ServeStaticMiddleware } from './index'; | ||
import { expect } from 'chai'; | ||
|
||
describe('ServeStaticMiddleware', () => { | ||
let middleware: ServeStaticMiddleware; | ||
describe('properly configured', () => { | ||
beforeEach(() => { | ||
ServeStaticMiddleware.configure('./lerna.json', {}); | ||
middleware = new ServeStaticMiddleware(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(middleware).to.not.be.undefined; | ||
}); | ||
|
||
it('should have a function called resolve', () => { | ||
expect(middleware.resolve).to.be.instanceof(Function); | ||
}); | ||
|
||
it('should should return a middleware from calling resolve', () => { | ||
expect(middleware.resolve()).to.be.an.instanceof(Function); | ||
}); | ||
|
||
afterEach(() => { | ||
ServeStaticMiddleware.configure(undefined); | ||
}); | ||
}); | ||
|
||
describe('not configured', () => { | ||
middleware = new ServeStaticMiddleware(); | ||
it('should should return a middleware from calling resolve', () => { | ||
expect(middleware.resolve.bind(middleware)).to.throw(Error); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as serveStatic from 'serve-static'; | ||
|
||
import { Middleware, NestMiddleware } from '@nestjs/common'; | ||
|
||
import { RequestHandler } from 'express'; | ||
|
||
@Middleware() | ||
export class ServeStaticMiddleware implements NestMiddleware { | ||
|
||
public static configure(root: string, opts?: serveStatic.ServeStaticOptions){ | ||
this.root = root; | ||
this.options = opts; | ||
} | ||
|
||
private static root: string; | ||
private static options: serveStatic.ServeStaticOptions; | ||
|
||
public resolve(...args: any[]): RequestHandler { | ||
if (ServeStaticMiddleware.root) { | ||
return serveStatic(ServeStaticMiddleware.root, ServeStaticMiddleware.options); | ||
} else { | ||
throw new Error('ServeStaticMiddleware requires a root in configure.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@nest-middlewares/serve-static", | ||
"version": "1.0.0", | ||
"description": "NestJS Middleware for Serve Static", | ||
"main": "index", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/wbhob/nest-middlewares.git" | ||
}, | ||
"keywords": [ | ||
"nest", | ||
"middlewares", | ||
"express", | ||
"node" | ||
], | ||
"author": "Wilson Hobbs <wilsonhobbs1@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/wbhob/nest-middlewares/issues" | ||
}, | ||
"homepage": "https://github.com/wbhob/nest-middlewares#readme", | ||
"peerDependencies": { | ||
"@nestjs/common": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"serve-static": "^1.13.1" | ||
} | ||
} |