简体   繁体   English

在 nodejs 的 typescript 类中使用路由器对象时出现异常

[英]Exception while using a router object in a typescript class in nodejs

I have a nodejs service in typescript.我在打字稿中有一个 nodejs 服务。 I'm trying to set a separate routing layer in the application.我正在尝试在应用程序中设置一个单独的路由层。

I have a app.js file where in我有一个 app.js 文件在哪里

let IndividualRoute= require('./routing/IndividualRoute');
app.use('/users', IndividualRoute);

I have create a IndividualRoute.ts file where im trying to use router object like below我创建了一个 IndividualRoute.ts 文件,我在其中尝试使用如下所示的路由器对象

import express = require('express');
import individualValidation from '../users/IndividualValidation';
let router = express.Router();

class IndividualRoute {
         router.get('/individualList', individualValidation.list);
}

export = IndividualRoute;

When i try to do the above, i get the below exception当我尝试执行上述操作时,出现以下异常

在此处输入图片说明

I tried the below things我尝试了以下事情

router.get('/individualList', (request, response) => {
        response.send('Hello world!');
});

But i get the same issue.但我遇到了同样的问题。 Also if i remove the class and have a simple file without class then i don't get this exception.此外,如果我删除类并有一个没有类的简单文件,那么我不会收到此异常。

Basically what im trying to do is, if the route matches it should hit the respective function in the IndividualValidation file like in below case基本上我想要做的是,如果路由匹配,它应该点击 IndividualValidation 文件中的相应函数,如下例所示

router.get('/individualList', individualValidation.list);

If the route is users/individualList it should hit individualValidation.list function with the request data.如果路由是 users/individualList,它应该使用请求数据点击 individualValidation.list 函数。 Basically im trying to do what is marked as answer in the below link基本上我正在尝试做以下链接中标记为答案的操作

express.Router() vs. app.get express.Router() 与 app.get

Any suggestion what i'm doing wrong.任何建议我做错了什么。

You're writing a class.你正在写一个类。 Inside a class, put the constructor and methods.在一个类中,放置构造函数和方法。 You can't write code directly here.这里不能直接写代码。 The line you wrote needs to be in the constructor or in a method.您编写的行需要在构造函数或方法中。

class IndividualRoute {
      constructor(){
         router.get('/individualList', individualValidation.list);
      }
}

In IndividualRoute.ts , please make the following changes:IndividualRoute.ts ,请进行以下更改:

import { Router } from 'express';
import individualValidation from '../users/IndividualValidation';
let router = Router();

router.get('/individualList', individualValidation.list);

export default router;

Also, if using typescript, why you have app.js?另外,如果使用打字稿,为什么要使用 app.js? I'll suggest using typescript for that file also.我建议也为该文件使用打字稿。 One more suggestion would be to have index.ts in your routing folder with code:另一种建议是在您的routing文件夹中使用 index.ts 代码:

import { Router } from express;
import IndividualRoute from "./IndividualRoute";

let router = Router();
router.use("/users", IndividualRoute); // set this for each individual route
/*
 * e.g.
 * router.use("/auth", auth);
*/

export default router;

So in your app.ts , all you have to do is:所以在你的app.ts ,你所要做的就是:

import * as express from "express";
import routes from "./routing";

let app = express();
app.use(routes);

// other code

I hope you find this helpful!我希望你觉得这有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM