Skip to content

Commit 3d33283

Browse files
author
Rumon
committed
feat(core): expose app.locals to express adapter
1 parent 5aeb40b commit 3d33283

6 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NestExpressApplication } from '@nestjs/platform-express';
2+
import { Test, TestingModule } from '@nestjs/testing';
3+
import { expect } from 'chai';
4+
import * as request from 'supertest';
5+
import { AppModule } from '../src/app.module';
6+
7+
describe('App-level globals (Express Application)', () => {
8+
let moduleFixture: TestingModule;
9+
let app: NestExpressApplication;
10+
11+
beforeEach(async () => {
12+
moduleFixture = await Test.createTestingModule({
13+
imports: [AppModule],
14+
}).compile();
15+
});
16+
17+
beforeEach(() => {
18+
app = moduleFixture.createNestApplication<NestExpressApplication>();
19+
});
20+
21+
it('should get "title" from "app.locals"', async () => {
22+
app.setLocal('title', 'My Website');
23+
await app.init();
24+
const response = await request(app.getHttpServer()).get('/').expect(200);
25+
expect(response.body.title).to.equal('My Website');
26+
});
27+
28+
it('should get "email" from "app.locals"', async () => {
29+
app.setLocal('email', 'admin@example.com');
30+
await app.listen(4444);
31+
const response = await request(app.getHttpServer()).get('/').expect(200);
32+
expect(response.body.email).to.equal('admin@example.com');
33+
});
34+
35+
afterEach(async () => {
36+
await app.close();
37+
});
38+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Controller, Get, Req } from '@nestjs/common';
2+
import { Request } from 'express';
3+
4+
@Controller()
5+
export class AppController {
6+
@Get()
7+
getGlobals(@Req() req: Request) {
8+
return req.app.locals;
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { AppController } from './app.controller';
3+
4+
@Module({
5+
controllers: [AppController],
6+
})
7+
export class AppModule {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": false,
5+
"noImplicitAny": false,
6+
"removeComments": true,
7+
"noLib": false,
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"target": "es6",
11+
"sourceMap": true,
12+
"allowJs": true,
13+
"outDir": "./dist"
14+
},
15+
"include": [
16+
"src/**/*",
17+
"e2e/**/*"
18+
],
19+
"exclude": [
20+
"node_modules"
21+
]
22+
}

packages/platform-express/adapters/express-adapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export class ExpressAdapter extends AbstractHttpAdapter {
142142
.forEach(parserKey => this.use(parserMiddleware[parserKey]));
143143
}
144144

145+
public setLocal(key: string, value: any) {
146+
this.instance.locals[key] = value;
147+
return this;
148+
}
149+
145150
public getType(): string {
146151
return 'express';
147152
}

packages/platform-express/interfaces/nest-express-application.interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,16 @@ export interface NestExpressApplication extends INestApplication {
7575
* @returns {this}
7676
*/
7777
setViewEngine(engine: string): this;
78+
79+
/**
80+
* Sets app-level globals for view templates
81+
*
82+
* @example
83+
* app.setLocal('title', 'My Site')
84+
*
85+
* @see https://expressjs.com/en/4x/api.html#app.locals
86+
*
87+
* @returns {this}
88+
*/
89+
setLocal(key: string, value: any): this;
7890
}

0 commit comments

Comments
 (0)