0

I looked all over the internet and all I could find was: "Make sure you add module.exports = router" at the end of your router file.

My router file is as follows:

let router = express.Router();

const staticOptions = {
  root: path.join(__dirname, "./../../assets")
};

// a bunch of router.get() functions

module.exports = router;
// auth/google

I then go ahead and import it into my server.ts file as follows:


import * as googleAuthRouter from "./api/AccountLinking/googleAuth";
...

app.use("/auth/google", googleAuthRouter);

but I'm still getting the following error:

TypeError: Router.use() requires a middleware function but got a Object

I only have one route im importing into the server.js file. I barely just started this project. Everywhere I look online says: "you just need to do module.exports = router and it'll fix it." In this case it doesn't. What am i doing wrong?

Typescript is driving me crazy and seriously hindering my development efforts. I'm fairly close to giving up and going back to plain JS

hundred_dolla_tea
  • 523
  • 1
  • 5
  • 14
  • Does this answer your question? [TypeError: Router.use() requires middleware function but got a Object](https://stackoverflow.com/questions/27465850/typeerror-router-use-requires-middleware-function-but-got-a-object) – Linda Lawton - DaImTo Dec 14 '20 at 10:36
  • Unfortunately not. app.router is depreciated in express 3.0+, as one of the answers there says. The other answers simply suggested what I wrote above: using module.exports = router, which isn't working – hundred_dolla_tea Dec 14 '20 at 10:49

1 Answers1

1

Change import to require:

const googleAuthRouter = require('./api/AccountLinking/googleAuth');

app.use("/auth/google", googleAuthRouter);
Yuriy Vorobyov
  • 755
  • 4
  • 8
  • I went ahead and changed it to import googleAuthRouter = require("./api/AccountLinking/googleAuth"); and it worked. Your solution would most likely work as well, so i picked it as the solution. Thank you Yuri – hundred_dolla_tea Dec 14 '20 at 16:24