我正在开发的nodejs应用程序我在控制器上使用swagger和typescript

const q_mapping = require('../../config/q_mapping');
import { amqpMessenger } from '../tools/amqp';

const survey = {
  surveyServiceCheck : (req,res) =>{
       amqpMessenger({serviceCheck : true},res, q_mapping.survey);
  }
}

export {survey}


sw着抱怨


  无法解析已配置的swagger-router处理程序:
  survey_surveyServiceCheck


当我看到生成的js文件时,它会导出类似的内容

"use strict";
var q_mapping = require('../../config/q_mapping');
var amqp_1 = require("../tools/amqp");
var survey = (function () {
    function survey() {
    }
    survey.prototype.surveyServiceCheck = function (req, res) {
        amqp_1.amqpMessenger({ serviceCheck: true }, res, q_mapping.survey);
    };
    return survey;
}());
exports.survey = survey;


当我在菜单上更改为exports.survey = survey;时,输入module.exports = survey;会大摇大摆地击中控制器。

我需要在打字稿上进行更改以生成上述结果,还是写控制器时做错了什么?
 我的挥霍定义

/survey:
    # binds a127 app logic to a route
    x-swagger-router-controller: survey
    get:
      description:  Check whether the survey service is up or not
      # used as the method name of the controller
      operationId: surveyServiceCheck
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/defaultResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

最佳答案

首先,我想survey不是const而是class。否则,TypeScript编译器不会创建函数对象作为survey的值。除此之外,您应该使用export =documentation

const q_mapping = require('../../config/q_mapping');
import { amqpMessenger } from '../tools/amqp';

class survey {
  surveyServiceCheck(req,res) {
    amqpMessenger({serviceCheck : true},res, q_mapping.survey);
  }
}

export = survey


生成的JavaScript文件如下所示:

"use strict";
var q_mapping = require('../../config/q_mapping');
var amqp_1 = require("../tools/amqp");
var survey = (function () {
    function survey() {
    }
    survey.prototype.surveyServiceCheck = function (req, res) {
        amqp_1.amqpMessenger({ serviceCheck: true }, res, q_mapping.survey);
    };
    return survey;
}());
module.exports = survey;


这等效于您发布的生成文件,包括您必须手动进行的修复。

关于node.js - 找不到NodeJs Swagger Typescript Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41822929/

10-10 00:46