nest icon indicating copy to clipboard operation
nest copied to clipboard

Middleware in ES2015

Open juandav opened this issue 9 years ago • 3 comments

I have a question about the use of middleware in ES2015. Typescript accepts interfaces but javascript does not, what can be done about it?

This in ES2015 cannot be done, what would be the correct syntax? For example in the documentation this:


import { Middleware, NestMiddleware } from '@nestjs/common';

@Middleware()
export class LoggingMiddleware implements NestMiddleware {
    resolve(): (req, res, next) => void {
        return (req, res, next) => {
            console.log('Request...');
            next();
        }
    }
}

juandav avatar Jun 25 '17 21:06 juandav

what can be done about it?

Nothing, this is only checked by typescript compiler.

@juandav Check how the NestMiddleware interface is compiled to es6:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

This is the whole file called nest-middleware.interface.js

Btw I recommend to write code in TypeScript using nest.js is perfect combination.

Regards.

cojack avatar Jun 26 '17 18:06 cojack

Hi @juandav, As @cojack mentioned - just remove typings. In your case:

mport { Middleware, NestMiddleware } from '@nestjs/common';

@Middleware()
export class LoggingMiddleware {
    resolve() {
        return (req, res, next) => {
            console.log('Request...');
            next();
        }
    }
}

kamilmysliwiec avatar Jul 06 '17 21:07 kamilmysliwiec

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.

lock[bot] avatar Sep 25 '19 12:09 lock[bot]