Require vs Import of Express Router to Prefix Routes

367
July 23, 2017, at 4:51 PM

I'm just starting out with Node using Express, and trying to use what I've learnt of Typescript in Angular by using TypeScript with Node. Following MDN's Express Route example (about half way down) so I can prefix a set of routes with /auth. They use require('./routes/auth'); to get the exported router as I understood it using import * as authRoutes from './routes/auth'; was equivalent based on docs and stackoverflow answers. This apparently isn't true since I can't do this:

import * as authRoutes from './routes/auth';
app.use('/auth', authRoutes); // Does not work?

VSCode highlights the '/auth' param and show this error [ts] Argument of type '"/auth"' is not assignable to parameter of type 'RequestHandlerParams'.

while this does work:

const authRoutes = require('./routes/auth');
app.use('/auth', authRoutes); // Does work!

Routes from MDN Example:

import * as express from 'express';
import { Router, Request, Response, NextFunction } from 'express';
import * as authController from '../controllers/auth.controller';
const router: Router = express.Router();
router.get('/register', (req: Request, res: Response, next: NextFunction) => {
  authController.register(req, res);
});
router.get('/login', (req: Request, res: Response, next: NextFunction) => {
  authController.login(req, res);
});
module.exports = router;

Can someone explain at a very high-level what I'm missing I'm finding what could be answers in blogs etc, but they are obviously not explained in a way that helps my understanding.

READ ALSO
nodejs : error: callback is not a function

nodejs : error: callback is not a function

I tried to do a callback because i need to call this function in another file but i dont undrestand what's wrong

378
Freeze existing properties of object, but allow object to be extensible

Freeze existing properties of object, but allow object to be extensible

So I have been looking at Objectfreeze() and Object

274
JSON from get request not returning properly

JSON from get request not returning properly

I'm running a Heroku server and I'm trying to change the innerHTML on my website to display certain statistics gathered from an endpoint on the server:

344
Promise.all not working with asynchronous inner function

Promise.all not working with asynchronous inner function

I have a list of items (dishes) that i need to get the owners of each item(dish) from mongodbI am using promises to execute this like so

304