Nest: CORS Demo?

Created on 23 Sep 2017  路  11Comments  路  Source: nestjs/nest

type question 馃檶

Most helpful comment

You could also just in main.ts

```typescript
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
import * as cors from 'cors';

async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.use(cors());
await app.listen(5000);
}
bootstrap();

All 11 comments

Not active. use v4
Middleware ->
image

ApplicationModule ->
image

Hey @Feng-JY,
As far as I know, everything is working correctly (the middlewares are applied). By the way, you can use https://github.com/expressjs/cors (cors package) and then just call app.use(cors()).

@kamilmysliwiec 100% works I'm using it in my project
https://github.com/illuminism/platform/blob/master/server/src/index.ts#L35

However, I think @Feng-JY wants to make it a middleware so he can scope a certain module and associated route(r/s) of the application. The way I did it, cors are open to all routes of the entire application, which isn't very robust.

@Feng-JY how about something more complex, like this:

```typescript
import { Middleware, NestMiddleware, ExpressMiddleware } from '@nestjs/common';
import * as cors from 'cors'

@Middleware()
export class CorsMiddleware implements NestMiddleware {
resolve(...args: any[]): ExpressMiddleware {
return (req, res, next) => {
cors({}, req, res, next);
};
}
}```

Furthermore, I think functional middleware fits better here.

Hi @Feng-JY,
Are you still having some troubles with this middleware?

Hi!

I just to use the @matthewharwood link to use cors() and it works!.

Although, would be interesting a little reference in the documentation to "https://github.com/expressjs/cors" ;-)

Thanks.

You could also just in main.ts

```typescript
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
import * as cors from 'cors';

async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.use(cors());
await app.listen(5000);
}
bootstrap();

@cojack although it seems very nice and clean, it does not work for me. Function cors is never called.
Try this:

import { Middleware, NestMiddleware, ExpressMiddleware } from '@nestjs/common';
import * as cors from 'cors';

@Middleware()
export class CorsMiddleware implements NestMiddleware {
    resolve(...args: any[]): ExpressMiddleware {
        return cors();
    }
}

You can pass options as a first argument of cors().

This lib makes this a breeze - https://github.com/wbhob/nest-middlewares

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KamGor picture KamGor  路  3Comments

cdiaz picture cdiaz  路  3Comments

mishelashala picture mishelashala  路  3Comments

artaommahe picture artaommahe  路  3Comments

rlesniak picture rlesniak  路  3Comments