Skip to content

Commit

Permalink
Static assets
Browse files Browse the repository at this point in the history
Updated readme
  • Loading branch information
seidelmartin committed Mar 14, 2020
1 parent 8154688 commit bee4519
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 23 deletions.
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ It consists of `KoaAdapter` which is basically just mapping between Nest server

### Missing parts

- Static assets are not yet implemented
- Setting view engine is not implemented
- Rendering of static pages is not yet implemented
- CORS settings are not yet implemented
- Not found handler is not yet implemented

## How to use

Expand Down Expand Up @@ -68,3 +65,45 @@ class TestModule {
}
}
```

#### CORS

To use CORS you'll have to install [`@koa/cors`](https://github.com/koajs/cors) package.

```
npm install @koa/cors
```

After installation is done you can set CORS same way as in normal NEST application.

> The `enableCors` method accepts options same as normal Nest application. The only difference is in `origin` property which should not be function.
```typescript
const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

app.enableCors();

await app.init();
```

#### Static assets

To use static assets you have to install [`koa-static`](https://github.com/koajs/static) package.

```
npm install koa-static
```

Once you have the dependency installed you can set you static assets folder.

```typescript
const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

app.useStaticAssets(path.join(__dirname, 'static'));

app.useStaticAssets(path.join(__dirname, 'static'), options);

await app.init();
```

> The `useStaticAssets` method also accepts [options](https://github.com/koajs/static#options) which are exactly same as those from `koa-static`.
148 changes: 132 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"@nestjs/common": "^6.11.8",
"@nestjs/core": "^6.11.8"
},
"optionalDependencies": {
"@koa/cors": "^3.0.0",
"koa-static": "^5.0.0"
},
"devDependencies": {
"@koa/cors": "^3.0.0",
"@nestjs/common": "^6.11.11",
Expand All @@ -42,15 +46,17 @@
"@types/koa": "^2.11.2",
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-router": "^7.4.0",
"@types/koa-static": "^4.0.1",
"@types/koa__cors": "^3.0.1",
"@types/mocha": "^7.0.2",
"@types/supertest": "^2.0.8",
"@typescript-eslint/eslint-plugin": "^2.22.0",
"@typescript-eslint/parser": "^2.22.0",
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-mocha": "^6.3.0",
"eslint-plugin-prettier": "^3.1.2",
"koa-static": "^5.0.0",
"mocha": "^7.1.0",
"np": "^6.2.0",
"prettier": "^1.19.1",
Expand Down
10 changes: 8 additions & 2 deletions src/KoaAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { nestToKoaMiddleware } from './NestKoaMiddleware';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { KoaCorsOptions } from './KoaCorsOptions';
import { Options as ServeStaticOptions } from 'koa-static';

type HttpMethods =
| 'all'
Expand Down Expand Up @@ -170,8 +171,13 @@ export class KoaAdapter extends AbstractHttpAdapter<
this.httpServer = http.createServer(this.getInstance<Koa>().callback());
}

public useStaticAssets(...args: any[]): any {
// TODO https://www.npmjs.com/package/koa-static
public useStaticAssets(path: string, options?: ServeStaticOptions): any {
const serveStaticMiddleware = loadPackage(
'koa-static',
'KoaAdapter.useStaticAssets()',
);

this.getInstance<Koa>().use(serveStaticMiddleware(path, options));
}

public setViewEngine(engine: string): any {
Expand Down
3 changes: 3 additions & 0 deletions src/NestKoaApplication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { INestApplication } from '@nestjs/common';
import { Middleware } from 'koa';
import { Options as ServeStaticOptions } from 'koa-static';

export interface NestKoaApplication extends INestApplication {
use(...middleware: Middleware[]): this;

useStaticAssets(path: string, options?: ServeStaticOptions): this;
}
52 changes: 52 additions & 0 deletions test/StaticAssets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { KoaAdapter, NestKoaApplication } from '../src';
import { Test } from '@nestjs/testing';
import { HelloWorldController } from './utils/HelloWorldController';
import { join } from 'path';
import fs from 'fs';
import { promisify } from 'util';
import supertest from 'supertest';
import assert from 'assert';

const readFile = promisify(fs.readFile);

describe.only('Static assets', () => {
let app: NestKoaApplication;
let file: Buffer;

before(async () => {
file = await readFile(join(__dirname, 'sample-data/static/image.png'));
});

beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [HelloWorldController],
}).compile();

app = module.createNestApplication(new KoaAdapter());
app.useStaticAssets(join(__dirname, 'sample-data/static/'));

await app.init();
});

afterEach(async () => {
await app.close();
});

it('should serve static assets from defined folder', async () => {
return supertest(app.getHttpServer())
.get('/image.png')
.expect(200)
.expect(res => {
assert(file.equals(res.body));
});
});

it('should still serve data from endpoints', async () => {
return supertest(app.getHttpServer())
.get('/hello/world/sync')
.expect(200)
.expect({
hello: 'world-sync',
});
});
});
Binary file added test/sample-data/static/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bee4519

Please sign in to comment.