jkyberneees / hydra-integration

Minimalist 'hydra integration' module, intended for Node.js web frameworks integration.
MIT License
14 stars 4 forks source link

[express] Middlewares & Error Handlers issue #1

Closed drosi94 closed 7 years ago

drosi94 commented 7 years ago

Hello i'm trying to register middlewares and error handlers to my express API using hydra integration but it doesnt work.

I don't get any response back if error occures and in success i dont get the headers that I register

This is how i'm tring to do this

class AuthenticationMicroService {
  public app: express.Application;

  constructor() {
    this.app = express();

    factory.init().then(factory => factory.getService(service => {
      this.registerMiddlewareCallback();
      this.registerRouteCallbacks(service);
    }))
      .catch(err => console.log('err', err));
  }

  /**
   * Load configuration file and initialize hydraExpress app
   */
  registerMiddlewareCallback() {

    sequelize.sync();

    this.app.use(helmet());

    this.app.use(morgan('dev'));

    this.app.use((req, res, next) => {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Headers', 'Origin, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Response-Time, X-PINGOTHER, X-CSRF-Token, Authorization');
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
      next();
    });

    this.app.use(bodyParser.json());
    this.app.use(bodyParser.urlencoded({extended: false, limit: '50mb'}));
    this.app.use(cookieParser());

    this.app.use(passport.initialize());
    require('./middlewares/strategies/index')(passport);

  }

  registerRouteCallbacks(service) {
    service.use('/internal/authentication', new AuthenticationAPI(new AuthenticationService()).createRoutes());
    // development error handler
    // will print stacktrace
    if (this.app.get('env') === 'development') {
      this.app.use((err, req, res) => {
        Utils.sendError(Utils.ServerResponse, res, err)
      });
    }

    // production error handler
    // no stacktraces leaked to user
    this.app.use((err, req, res) => {
      Utils.sendError(Utils.ServerResponse, res, err)
    });
  }

}
drosi94 commented 7 years ago

Fixed!