mirror of
https://github.com/nestjs/docs.nestjs.com.git
synced 2026-02-25 22:15:07 +00:00
docs: update listen to use port env variable
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
"trailingComma": "all",
|
||||
"printWidth": 80
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ async function bootstrap() {
|
||||
|
||||
await app.close();
|
||||
} else {
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -232,9 +232,7 @@ const publishOptions = {
|
||||
sha: process.env.CI_COMMIT_SHA,
|
||||
target: process.env.CI_MERGE_REQUEST_DIFF_BASE_SHA,
|
||||
trigger: process.env.CI_MERGE_REQUEST_DIFF_BASE_SHA ? 'pull' : 'push',
|
||||
branch:
|
||||
process.env.CI_COMMIT_BRANCH ??
|
||||
process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME,
|
||||
branch: process.env.CI_COMMIT_BRANCH ?? process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME,
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
snapshot: true,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -185,7 +185,7 @@ This page comes in handy when you want to identify potential issues in your appl
|
||||
To save a serialized graph to a file, use the following code:
|
||||
|
||||
```typescript
|
||||
await app.listen(3000); // OR await app.init()
|
||||
await app.listen(process.env.PORT ?? 3000); // OR await app.init()
|
||||
fs.writeFileSync('./graph.json', app.get(SerializedGraph).toString());
|
||||
```
|
||||
|
||||
|
||||
@@ -141,7 +141,10 @@ Nest provides a set of standard exceptions that inherit from the base `HttpExcep
|
||||
All the built-in exceptions can also provide both an error `cause` and an error description using the `options` parameter:
|
||||
|
||||
```typescript
|
||||
throw new BadRequestException('Something bad happened', { cause: new Error(), description: 'Some error description' })
|
||||
throw new BadRequestException('Something bad happened', {
|
||||
cause: new Error(),
|
||||
description: 'Some error description',
|
||||
});
|
||||
```
|
||||
|
||||
Using the above, this is how the response would look:
|
||||
@@ -150,7 +153,7 @@ Using the above, this is how the response would look:
|
||||
{
|
||||
"message": "Something bad happened",
|
||||
"error": "Some error description",
|
||||
"statusCode": 400,
|
||||
"statusCode": 400
|
||||
}
|
||||
```
|
||||
|
||||
@@ -278,7 +281,7 @@ To create a global-scoped filter, you would do the following:
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
@@ -394,7 +397,7 @@ async function bootstrap() {
|
||||
const { httpAdapter } = app.get(HttpAdapterHost);
|
||||
app.useGlobalFilters(new AllExceptionsFilter(httpAdapter));
|
||||
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -18,7 +18,7 @@ async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
forceCloseConnections: true,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
|
||||
bootstrap();
|
||||
|
||||
@@ -10,7 +10,7 @@ const httpsOptions = {
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
httpsOptions,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
If you use the `FastifyAdapter`, create the application as follows:
|
||||
@@ -33,10 +33,7 @@ const httpsOptions = {
|
||||
};
|
||||
|
||||
const server = express();
|
||||
const app = await NestFactory.create(
|
||||
AppModule,
|
||||
new ExpressAdapter(server),
|
||||
);
|
||||
const app = await NestFactory.create(AppModule, new ExpressAdapter(server));
|
||||
await app.init();
|
||||
|
||||
const httpServer = http.createServer(server).listen(3000);
|
||||
@@ -56,7 +53,8 @@ export class ShutdownObserver implements OnApplicationShutdown {
|
||||
|
||||
public async onApplicationShutdown(): Promise<void> {
|
||||
await Promise.all(
|
||||
this.httpServers.map((server) =>
|
||||
this.httpServers.map(
|
||||
(server) =>
|
||||
new Promise((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error) {
|
||||
@@ -65,7 +63,7 @@ export class ShutdownObserver implements OnApplicationShutdown {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
})
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import { AppModule } from './app.module';
|
||||
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
||||
rawBody: true,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
To access the raw request body in a controller, a convenience interface `RawBodyRequest` is provided to expose a `rawBody` field on the request: use the interface `RawBodyRequest` type:
|
||||
@@ -77,7 +77,7 @@ const app = await NestFactory.create<NestFastifyApplication>(
|
||||
rawBody: true,
|
||||
},
|
||||
);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
To access the raw request body in a controller, a convenience interface `RawBodyRequest` is provided to expose a `rawBody` field on the request: use the interface `RawBodyRequest` type:
|
||||
|
||||
@@ -37,7 +37,7 @@ import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule, { logger: ['error'] });
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@switch
|
||||
@@ -65,7 +65,7 @@ import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -84,7 +84,7 @@ async function bootstrap() {
|
||||
// Starts listening for shutdown hooks
|
||||
app.enableShutdownHooks();
|
||||
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -22,7 +22,6 @@ You implement custom Nest middleware in either a function, or in a class with an
|
||||
|
||||
> warning **Warning** `Express` and `fastify` handle middleware differently and provide different method signatures, read more [here](/techniques/performance#middleware).
|
||||
|
||||
|
||||
```typescript
|
||||
@@filename(logger.middleware)
|
||||
import { Injectable, NestMiddleware } from '@nestjs/common';
|
||||
@@ -132,7 +131,10 @@ export class AppModule {
|
||||
Pattern based routes are supported as well. For instance, the asterisk is used as a **wildcard**, and will match any combination of characters:
|
||||
|
||||
```typescript
|
||||
forRoutes({ path: 'ab*cd', method: RequestMethod.ALL });
|
||||
forRoutes({
|
||||
path: 'ab*cd',
|
||||
method: RequestMethod.ALL,
|
||||
});
|
||||
```
|
||||
|
||||
The `'ab*cd'` route path will match `abcd`, `ab_cd`, `abecd`, and so on. The characters `?`, `+`, `*`, and `()` may be used in a route path, and are subsets of their regular expression counterparts. The hyphen ( `-`) and the dot (`.`) are interpreted literally by string-based paths.
|
||||
@@ -245,7 +247,7 @@ If we want to bind middleware to every registered route at once, we can use the
|
||||
@@filename(main)
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.use(logger);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
> info **Hint** Accessing the DI container in a global middleware is not possible. You can use a [functional middleware](middleware#functional-middleware) instead when using `app.use()`. Alternatively, you can use a class middleware and consume it with `.forRoutes('*')` within the `AppModule` (or any other module).
|
||||
|
||||
@@ -32,7 +32,7 @@ async function bootstrap() {
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('api', app, document);
|
||||
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
@@ -62,14 +62,15 @@ As you can see, the `SwaggerModule` automatically reflects all of your endpoints
|
||||
|
||||
> info **Hint** To generate and download a Swagger JSON file, navigate to `http://localhost:3000/api-json` (assuming that your Swagger documentation is available under `http://localhost:3000/api`).
|
||||
> It is also possible to expose it on a route of your choice using only the setup method from `@nestjs/swagger`, like this:
|
||||
>
|
||||
> ```typescript
|
||||
> SwaggerModule.setup('swagger', app, document, {
|
||||
> jsonDocumentUrl: 'swagger/json',
|
||||
> });
|
||||
> ```
|
||||
>
|
||||
> Which would expose it at `http://localhost:3000/swagger/json`
|
||||
|
||||
|
||||
> warning **Warning** When using `fastify` and `helmet`, there may be a problem with [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP), to solve this collision, configure the CSP as shown below:
|
||||
>
|
||||
> ```typescript
|
||||
|
||||
@@ -75,7 +75,7 @@ async function bootstrap() {
|
||||
});
|
||||
SwaggerModule.setup('api/dogs', app, dogDocument);
|
||||
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -430,7 +430,7 @@ Since the `ValidationPipe` was created to be as generic as possible, we can real
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.useGlobalPipes(new ValidationPipe());
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -60,7 +60,7 @@ declare const module: any;
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
|
||||
if (module.hot) {
|
||||
module.hot.accept();
|
||||
@@ -127,10 +127,7 @@ module.exports = {
|
||||
resolve: {
|
||||
extensions: ['.tsx', '.ts', '.js'],
|
||||
},
|
||||
plugins: [
|
||||
new webpack.HotModuleReplacementPlugin(),
|
||||
new RunScriptWebpackPlugin({ name: 'server.js', autoRestart: false }),
|
||||
],
|
||||
plugins: [new webpack.HotModuleReplacementPlugin(), new RunScriptWebpackPlugin({ name: 'server.js', autoRestart: false })],
|
||||
output: {
|
||||
path: path.join(__dirname, 'dist'),
|
||||
filename: 'server.js',
|
||||
@@ -151,7 +148,7 @@ declare const module: any;
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
|
||||
if (module.hot) {
|
||||
module.hot.accept();
|
||||
|
||||
@@ -9,7 +9,7 @@ To enable CORS, call the `enableCors()` method on the Nest application object.
|
||||
```typescript
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.enableCors();
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
The `enableCors()` method takes an optional configuration object argument. The available properties of this object are described in the official [CORS](https://github.com/expressjs/cors#configuration-options) documentation. Another way is to pass a [callback function](https://github.com/expressjs/cors#configuring-cors-asynchronously) that lets you define the configuration object asynchronously based on the request (on the fly).
|
||||
@@ -19,5 +19,5 @@ Or, pass a [CORS configuration object](https://github.com/expressjs/cors#configu
|
||||
|
||||
```typescript
|
||||
const app = await NestFactory.create(AppModule, { cors: true });
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
@@ -21,7 +21,7 @@ To disable logging, set the `logger` property to `false` in the (optional) Nest
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger: false,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
To enable specific logging levels, set the `logger` property to an array of strings specifying the log levels to display, as follows:
|
||||
@@ -30,7 +30,7 @@ To enable specific logging levels, set the `logger` property to an array of stri
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger: ['error', 'warn'],
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
Values in the array can be any combination of `'log'`, `'fatal'`, `'error'`, `'warn'`, `'debug'`, and `'verbose'`.
|
||||
@@ -45,7 +45,7 @@ You can provide a custom logger implementation to be used by Nest for system log
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger: console,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
Implementing your own custom logger is straightforward. Simply implement each of the methods of the `LoggerService` interface as shown below.
|
||||
@@ -93,7 +93,7 @@ You can then supply an instance of `MyLogger` via the `logger` property of the N
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger: new MyLogger(),
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
This technique, while simple, doesn't utilize dependency injection for the `MyLogger` class. This can pose some challenges, particularly for testing, and limit the reusability of `MyLogger`. For a better solution, see the <a href="techniques/logger#dependency-injection">Dependency Injection</a> section below.
|
||||
@@ -148,7 +148,7 @@ const app = await NestFactory.create(AppModule, {
|
||||
bufferLogs: true,
|
||||
});
|
||||
app.useLogger(app.get(MyLogger));
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
> info **Note** In the example above, we set the `bufferLogs` to `true` to make sure all logs will be buffered until a custom logger is attached (`MyLogger` in this case) and the application initialisation process either completes or fails. If the initialisation process fails, Nest will fallback to the original `ConsoleLogger` to print out any reported error messages. Also, you can set the `autoFlushLogs` to `false` (default `true`) to manually flush logs (using the `Logger#flush()` method).
|
||||
@@ -249,7 +249,7 @@ const app = await NestFactory.create(AppModule, {
|
||||
bufferLogs: true,
|
||||
});
|
||||
app.useLogger(new MyLogger());
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
> info **Hint** Alternatively, instead of setting `bufferLogs` to `true`, you could temporarily disable the logger with `logger: false` instruction. Be mindful that if you supply `logger: false` to `NestFactory.create`, nothing will be logged until you call `useLogger`, so you may miss some important initialization errors. If you don't mind that some of your initial messages will be logged with the default logger, you can just omit the `logger: false` option.
|
||||
|
||||
@@ -33,7 +33,7 @@ async function bootstrap() {
|
||||
app.setBaseViewsDir(join(__dirname, '..', 'views'));
|
||||
app.setViewEngine('hbs');
|
||||
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@switch
|
||||
@@ -50,7 +50,7 @@ async function bootstrap() {
|
||||
app.setBaseViewsDir(join(__dirname, '..', 'views'));
|
||||
app.setViewEngine('hbs');
|
||||
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
@@ -156,7 +156,7 @@ async function bootstrap() {
|
||||
},
|
||||
templates: join(__dirname, '..', 'views'),
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@switch
|
||||
@@ -177,7 +177,7 @@ async function bootstrap() {
|
||||
},
|
||||
templates: join(__dirname, '..', 'views'),
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -34,7 +34,7 @@ async function bootstrap() {
|
||||
AppModule,
|
||||
new FastifyAdapter()
|
||||
);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
@@ -74,7 +74,6 @@ You can pass options into the Fastify constructor through the `FastifyAdapter` c
|
||||
new FastifyAdapter({ logger: true });
|
||||
```
|
||||
|
||||
|
||||
#### Middleware
|
||||
|
||||
Middleware functions retrieve the raw `req` and `res` objects instead of Fastify's wrappers. This is how the `middie` package works (that's used under the hood) and `fastify` - check out this [page](https://www.fastify.io/docs/latest/Reference/Middleware/) for more information,
|
||||
@@ -128,7 +127,6 @@ newFeature() {
|
||||
|
||||
> info **Hint** `@RouteConfig()` and `@RouteConstraints` are imported from `@nestjs/platform-fastify`.
|
||||
|
||||
|
||||
#### Example
|
||||
|
||||
A working example is available [here](https://github.com/nestjs/nest/tree/master/sample/10-fastify).
|
||||
|
||||
@@ -141,7 +141,7 @@ We'll start by binding `ValidationPipe` at the application level, thus ensuring
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.useGlobalPipes(new ValidationPipe());
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
```
|
||||
|
||||
@@ -40,7 +40,7 @@ const app = await NestFactory.create(AppModule);
|
||||
app.enableVersioning({
|
||||
type: VersioningType.URI,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
> warning **Notice** The version in the URI will be automatically prefixed with `v` by default, however the prefix value can be configured by setting the `prefix` key to your desired prefix or `false` if you wish to disable it.
|
||||
@@ -62,7 +62,7 @@ app.enableVersioning({
|
||||
type: VersioningType.HEADER,
|
||||
header: 'Custom-Header',
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
The `header` property should be the name of the header that will contain the version of the request.
|
||||
@@ -84,7 +84,7 @@ app.enableVersioning({
|
||||
type: VersioningType.MEDIA_TYPE,
|
||||
key: 'v=',
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
The `key` property should be the key and separator of the key-value pair that contains the version. For the example `Accept: application/json;v=2`, the `key` property would be set to `v=`.
|
||||
@@ -130,7 +130,7 @@ app.enableVersioning({
|
||||
type: VersioningType.CUSTOM,
|
||||
extractor,
|
||||
});
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
```
|
||||
|
||||
#### Usage
|
||||
|
||||
Reference in New Issue
Block a user