當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript IRoute.get方法代碼示例

本文整理匯總了TypeScript中express-serve-static-core.IRoute.get方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript IRoute.get方法的具體用法?TypeScript IRoute.get怎麽用?TypeScript IRoute.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在express-serve-static-core.IRoute的用法示例。


在下文中一共展示了IRoute.get方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
import {IRoute} from "express-serve-static-core";
import {bimModel} from "../models/models";
import {Stream} from "stream";
import {entityIO} from "../connections/socket";

const api: IRoute = router.route("/api/bim");

api.get(async (req: Request, res: Response) => {
    let stream: Stream = await bimModel
        .find({})
        .lean(true)
        .stream({ transform: JSON.stringify });

    res.type("application/json");

    stream.on("data", (doc: string) => {
        // res.write();
        entityIO.emit("entity", doc);
    }).on("error", (err: Error) => {
        console.log(err);
    }).on("close", () => {
        res.end(JSON.stringify({completed: true}));
    });
});

export const apiBim = router;
開發者ID:vildantursic,項目名稱:walter,代碼行數:28,代碼來源:bim.ts

示例2: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
import {IRoute} from "express-serve-static-core";
import {objectModel} from "../models/models";
import {Stream} from "stream";

const api: IRoute = router.route("/api/object");

api.get(async (req: Request, res: Response) => {
    let stream: Stream = await objectModel
        .find({})
        .stream({ transform: JSON.stringify });

    stream.on("data", (doc: Object) => {
        res.write(doc, "ascii");
    }).on("error", (err: Error) => {
        console.log(err);
    }).on("close", () => {
        res.end("Done");
    });
});

export const apiObjects = router;
開發者ID:vildantursic,項目名稱:walter,代碼行數:23,代碼來源:objects.ts

示例3: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
import {IRoute} from "express-serve-static-core";
import {complexModel} from "../models/models";
import {Stream} from "stream";
import {complexIO} from "../connections/socket";

const api: IRoute = router.route("/api/complex");

api.get(async (req: Request, res: Response) => {
    let stream: Stream = await complexModel
        .find({})
        .select({_id: 0, name: 1})
        .lean(true)
        .stream({ transform: JSON.stringify });

    stream.on("data", (doc: Object) => {
        res.write(doc, "ascii");
        complexIO.emit("complex", doc);
    }).on("error", (err: Error) => {
        console.log(err);
    }).on("close", () => {
        res.end("Done");
    });
});

export const apiComplexes = router;
開發者ID:vildantursic,項目名稱:walter,代碼行數:27,代碼來源:complexes.ts

示例4: checkIfDataIsArray

import {IRoute} from "express-serve-static-core";
import {complexModel} from "../models/models";
import {checkObjectIDValidity, checkIfDataIsArray} from "../helpers/validation";
import {errorIDValidationMessages, errorApiMessages} from "../helpers/errorMessages";
import {log} from "walter-logger";

const api: IRoute = router.route("/api/complex/:id");

api.get(async (req: Request, res: Response) => {
    let id: string = req.params.id;
    if (!checkObjectIDValidity(id)) {
        log.alert("alert", errorIDValidationMessages.getMessage, ["complex"]);
        res.status(400).json(errorIDValidationMessages.getMessage);
    }
    let obj: Object = await complexModel
        .find({_id: id})
        .exec()
        .catch((e: Error) => {
            log.error("error", errorIDValidationMessages.postMessage, ["complex"]);
            res.status(400).send({ error: errorApiMessages.postMessage + e });
        });

    res.json(obj);
});

api.post(async (req: Request, res: Response) => {

    let obj: Array<Object> = checkIfDataIsArray(req.body);

    let dataRes: Object = await complexModel.insertMany(obj);
    res.status(200).json(dataRes);
});
開發者ID:vildantursic,項目名稱:walter,代碼行數:32,代碼來源:complex.ts

示例5: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
import {IRoute} from "express-serve-static-core";
import {objectModel} from "../models/models";
import {checkObjectIDValidity, checkIfDataIsArray} from "../helpers/validation";
import {errorIDValidationMessages, errorApiMessages} from "../helpers/errorMessages";

const api: IRoute = router.route("/api/object/:id");

api.get(async (req: Request, res: Response) => {
    let id: string = req.params.id;
    if (!checkObjectIDValidity(id)) {
        res.status(400).json(errorIDValidationMessages.getMessage);
    }
    let obj: Object = await objectModel
        .find({_id: id})
        .exec();

    res.json(obj);
});

api.post(async (req: Request, res: Response) => {

    // check data before saving also could do validation within function
    let obj: Array<Object> = checkIfDataIsArray(req.body);

    let dataRes: Object = await objectModel.insertMany(obj);
    res.status(200).json(dataRes);
});

api.put(async (req: Request, res: Response) => {
開發者ID:vildantursic,項目名稱:walter,代碼行數:31,代碼來源:object.ts


注:本文中的express-serve-static-core.IRoute.get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。