0

I have the following express server:

import * as express from "express";
import * as bodyParser from "body-parser";
import * as mongoose from "mongoose";

import { Routes } from "./routes/transactions";
import { authMiddleware } from "./middleware/auth";
import { errorMiddleware } from "./middleware/error";

class App {
  public app: express.Application;
  public routeProvider: Routes = new Routes();
  public mongoUrl: string = "mongodb://...:...@mydomain.com:27017/mycollection"; // This should be read from github CLI

  constructor() {
    this.app = express();
    this.config();
    this.routeProvider.routes(this.app);
    this.mongoSetup();
  }

  private config(): void {
    this.app.use(bodyParser.json());
    this.app.use(bodyParser.urlencoded({ extended: false }));
    this.app.use(errorMiddleware);
    this.app.use(authMiddleware);
  }

  private mongoSetup(): void {
    mongoose.Promise = global.Promise;
    mongoose.connect(
      this.mongoUrl,
      { useNewUrlParser: true }
    );
  }
}

export default new App().app;

This is the authMiddleware (almost the same as the guide on the auth0 site for usage with node.js):

import * as jwt from "express-jwt";
import { expressJwtSecret } from "jwks-rsa";

export function authMiddleware() {
  return jwt({
    secret: expressJwtSecret({
      cache: true,
      rateLimit: true,
      jwksRequestsPerMinute: 5,
      jwksUri: "..."
    }),
    audience: "...",
    issuer: "...",
    algorithms: ["RS256"]
  });
}

And my error middleware:

import { Request, Response } from "express";

export function errorMiddleware(err, req: Request, res: Response, next) {
  console.error(err.stack);
  res.status(500).send("Something broke!");
}

Now what would expect to happen - since I broke all the auth and mongodb URLs on purpose, that I would get a status code 500 with the message Something broke!.

Instead I get the

Could not get any response

message when sending a requst using postman.

What am I doing wrong?

jenil christo
  • 666
  • 8
  • 16
Miha Šušteršič
  • 9,742
  • 25
  • 92
  • 163
  • what is the url that you hit from postman? If you are on a sub domain refer https://stackoverflow.com/questions/47806876/could-not-get-any-response-response-when-using-postman-with-subdomain – jenil christo Dec 28 '18 at 15:17
  • `localhost:3000/transaction` - the thing is if I remove the `authMiddleware` everything works as expected. Opening the `GET` route in browser instead of using postman times out as well. If you think it would help I can link the repo with the route and model definitions, but I do not think it matters here – Miha Šušteršič Dec 28 '18 at 15:20

0 Answers0