简体   繁体   English

如何使用打字稿扩展表达对象?

[英]How to extend express object using typescript?

When I compiled a typescript project I don't see express import import {Request, Response} . 编译打字稿项目时,没有看到express import import {Request, Response} Now I am trying to use require but not sure how to extend the express object and use req and res . 现在,我尝试使用require但不确定如何扩展express对象以及使用reqres Any help will be appreciated. 任何帮助将不胜感激。

index.ts 索引

// import {Request, Response} from 'express';
import express = require('express');
import * as sdkStore from "../../common/sdk-store";


export function getInfo(req: Request, res: Response) {

    var app = sdkStore.getSdkInstance(req.body.client);
    app.Payment.Info(req.body, function (result) {
        res.send(result);
    });
}

I am using the same strategy to use Request & Response from express module. 我正在使用相同的策略来使用express模块的RequestResponse

This is how I am doing it (check parameters of getTemples method)- 这就是我的做法(检查getTemples方法的参数)-

Typescript 打字稿

import * as express from "express";
import { Response as ApiResponse } from "../models/response.model";
import { TempleProvider } from "../providers/temple.provider";

export namespace TempleFacade {

    export function getTemples(req: express.Request, res: express.Response, next: express.NextFunction): void {
        let apiResponse: ApiResponse<any> = new ApiResponse();
        TempleProvider.getTemples()
            .then((response: Array<any>) => {
                apiResponse.data = response;
                res.json(apiResponse);
            })
            .catch((error: any) => {
                apiResponse.data = null;
                apiResponse.status = false;
                apiResponse.messages = error;
                res.json(apiResponse);
            });
    }
}

Compiled Javascript 编译的Javascript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const response_model_1 = require("../models/response.model");
const temple_provider_1 = require("../providers/temple.provider");
var TempleFacade;
(function (TempleFacade) {
    function getTemples(req, res, next) {
        let apiResponse = new response_model_1.Response();
        temple_provider_1.TempleProvider.getTemples()
            .then((response) => {
            apiResponse.data = response;
            res.json(apiResponse);
        })
            .catch((error) => {
            apiResponse.data = null;
            apiResponse.status = false;
            apiResponse.messages = error;
            res.json(apiResponse);
        });
    }
    TempleFacade.getTemples = getTemples;
})(TempleFacade = exports.TempleFacade || (exports.TempleFacade = {}));
//# sourceMappingURL=temple.facade.js.map

I hope this helps :) 我希望这有帮助 :)

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

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