# Locals Decorator operation input response
Module | import { Locals } from "@tsed/common" |
---|---|
Source | /packages/common/src/mvc/decorators/params/locals.ts |
# Overview
function Locals(expression: string): ParameterDecorator;
export function Locals(): ParameterDecorator;
Param | Type | Description |
---|---|---|
expression | string | The path of the property to get. |
# Description
Locals return the value from response.locals object.
TIP
Locals are generally used by express and third-party like templating engine to render a page/template. See Templating section for more details.
# Example
@Middleware()
class LocalsMiddleware {
use(@Locals() locals: any) {
// set some on locals
locals.user = "user"
}
}
@Controller('/')
@UseBefore(LocalsMiddleware)
class MyCtrl {
@Get('/')
@Render('home.ejs') // will use locals and returned data to render the page
get(@Locals('user') user: any) {
console.log('user', user);
return {
description: 'Hello world'
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing