Express.js HTTP platform adapter for NestJS applications with comprehensive file upload capabilities
npx @tessl/cli install tessl/npm-nestjs--platform-express@11.1.00
# NestJS Platform Express
1
2
NestJS Platform Express is the Express.js HTTP platform adapter for NestJS applications. It provides comprehensive Express integration including HTTP request/response handling, static file serving, view rendering, CORS support, body parsing, and a complete file upload system powered by Multer.
3
4
## Package Information
5
6
- **Package Name**: @nestjs/platform-express
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @nestjs/platform-express`
10
11
## Core Imports
12
13
```typescript
14
import {
15
ExpressAdapter,
16
NestExpressApplication,
17
getBodyParserOptions
18
} from '@nestjs/platform-express';
19
```
20
21
For file upload functionality:
22
23
```typescript
24
import {
25
FileInterceptor,
26
FilesInterceptor,
27
FileFieldsInterceptor,
28
MulterModule
29
} from '@nestjs/platform-express';
30
```
31
32
CommonJS:
33
34
```javascript
35
const { ExpressAdapter, NestExpressApplication } = require('@nestjs/platform-express');
36
```
37
38
## Basic Usage
39
40
```typescript
41
import { NestFactory } from '@nestjs/core';
42
import { NestExpressApplication } from '@nestjs/platform-express';
43
import { AppModule } from './app.module';
44
45
async function bootstrap() {
46
// Create Express-based NestJS application
47
const app = await NestFactory.create<NestExpressApplication>(AppModule);
48
49
// Configure Express-specific features
50
app.useStaticAssets('public');
51
app.setBaseViewsDir('views');
52
app.setViewEngine('hbs');
53
app.enableCors();
54
55
// Configure custom body parser
56
app.useBodyParser('json', { limit: '50mb' });
57
58
await app.listen(3000);
59
}
60
bootstrap();
61
```
62
63
## Architecture
64
65
NestJS Platform Express is built around several key components:
66
67
- **ExpressAdapter**: Core HTTP adapter extending NestJS's AbstractHttpAdapter with Express-specific implementations
68
- **NestExpressApplication**: Enhanced application interface providing Express-specific methods and configurations
69
- **File Upload System**: Complete Multer integration with interceptors for various upload scenarios
70
- **Type System**: Full TypeScript support with Express-specific interfaces and type definitions
71
- **Body Parser Integration**: Configurable Express body parsing with raw body support
72
73
## Capabilities
74
75
### Express HTTP Adapter
76
77
Core Express.js integration providing HTTP server functionality, middleware support, static file serving, view rendering, and lifecycle hooks.
78
79
```typescript { .api }
80
class ExpressAdapter extends AbstractHttpAdapter<http.Server | https.Server> {
81
constructor(instance?: any);
82
listen(port: string | number, callback?: () => void): Server;
83
close(): Promise<void>;
84
useStaticAssets(path: string, options: ServeStaticOptions): void;
85
setViewEngine(engine: string): void;
86
enableCors(options: CorsOptions | CorsOptionsDelegate<any>): void;
87
useBodyParser<Options>(
88
type: NestExpressBodyParserType,
89
rawBody: boolean,
90
options?: Options
91
): this;
92
}
93
94
function getBodyParserOptions<Options>(
95
rawBody: boolean,
96
options?: Options
97
): Options;
98
```
99
100
[Express HTTP Adapter](./express-adapter.md)
101
102
### Application Interface
103
104
TypeScript interfaces and types for Express-based NestJS applications, including application configuration, body parser options, and static file serving.
105
106
```typescript { .api }
107
interface NestExpressApplication<TServer = http.Server> extends INestApplication<TServer> {
108
getHttpAdapter(): HttpServer<Express.Request, Express.Response, Express>;
109
useStaticAssets(path: string, options?: ServeStaticOptions): this;
110
setBaseViewsDir(path: string | string[]): this;
111
setViewEngine(engine: string): this;
112
useBodyParser<Options = NestExpressBodyParserOptions>(
113
parser: NestExpressBodyParserType,
114
options?: Omit<Options, 'verify'>
115
): this;
116
enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void;
117
}
118
119
type NestExpressBodyParserType = 'json' | 'urlencoded' | 'text' | 'raw';
120
```
121
122
[Application Interface](./application-interface.md)
123
124
### File Upload System
125
126
Comprehensive file upload functionality using Multer with interceptors for single files, multiple files, file fields, and validation. Includes module configuration and error handling.
127
128
```typescript { .api }
129
function FileInterceptor(
130
fieldName: string,
131
localOptions?: MulterOptions
132
): Type<NestInterceptor>;
133
134
function FilesInterceptor(
135
fieldName: string,
136
maxCount?: number,
137
localOptions?: MulterOptions
138
): Type<NestInterceptor>;
139
140
function FileFieldsInterceptor(
141
uploadFields: MulterField[],
142
localOptions?: MulterOptions
143
): Type<NestInterceptor>;
144
145
class MulterModule {
146
static register(options?: MulterModuleOptions): DynamicModule;
147
static registerAsync(options: MulterModuleAsyncOptions): DynamicModule;
148
}
149
```
150
151
[File Upload System](./file-upload.md)
152
153
## Common Types
154
155
```typescript { .api }
156
interface ServeStaticOptions {
157
dotfiles?: string;
158
etag?: boolean;
159
extensions?: string[];
160
fallthrough?: boolean;
161
immutable?: boolean;
162
index?: boolean | string | string[];
163
lastModified?: boolean;
164
maxAge?: number | string;
165
redirect?: boolean;
166
setHeaders?: (res: any, path: string, stat: any) => any;
167
prefix?: string;
168
}
169
170
interface NestExpressBodyParserOptions {
171
inflate?: boolean;
172
limit?: number | string;
173
type?: string | string[] | ((req: any) => any);
174
[key: string]: any;
175
}
176
177
interface MulterOptions {
178
dest?: string;
179
storage?: any;
180
limits?: {
181
fieldNameSize?: number;
182
fieldSize?: number;
183
fields?: number;
184
fileSize?: number;
185
files?: number;
186
parts?: number;
187
headerPairs?: number;
188
};
189
preservePath?: boolean;
190
fileFilter?: (
191
req: any,
192
file: any,
193
callback: (error: Error | null, acceptFile: boolean) => void
194
) => void;
195
}
196
197
interface MulterField {
198
name: string;
199
maxCount?: number;
200
}
201
```