Recently, I have been following a tutorial on how to build a Node.js app with TypeScript. As part of the tutorial, I attempted to organize my routes by creating a separate route folder and a test.ts file containing the following code:
import {Router} from "express";
let router = Router();
router.get('/', get);
export async function get(req, res, next) {
try {
res.send("Testing...");
} catch (err) {
console.log('err', err);
next(err);
}
}
export {router};
I then tried to integrate this route into my server.ts file like so:
import * as express from "express";
import * as TestRoute from './routes/test';
class App {
public express : express.Application;
constructor() {
this.express = express();
this.mountRoutes();
}
private mountRoutes() : void {
const routes: express.Router = express.Router();
routes.use('/test', TestRoute);
this.express.use('/', routes);
}
}
export default new App().express;
However, when using routes.use('/test', TestRoute);
, TypeScript raised an error indicating that the types were not assignable. The error message specifically mentioned the missing 'includes' property.
I am seeking help in understanding and resolving this error. Additionally, I would appreciate any guidance on structuring routes and incorporating them into TypeScript applications. My current TypeScript version is 2.7.