• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript express-serve-static-core.IRoute类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中express-serve-static-core.IRoute的典型用法代码示例。如果您正苦于以下问题:TypeScript IRoute类的具体用法?TypeScript IRoute怎么用?TypeScript IRoute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了IRoute类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: 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


示例2: 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


示例3: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
import {IRoute} from "express-serve-static-core";
import {userModel} from "../models/models";
import * as jwt from "jsonwebtoken";
import {key} from "../helpers/key";

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

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

    let username: string = req.query.username;

    let obj: Object = await userModel
        .find({username: username})
        .lean()
        .exec();

    if (username === obj[0].username) {
        let token: string = jwt.sign({ username: username }, key, { expiresIn: "2h" });
        res.status(200).json({ token: token });
    } else {
        res.status(401).json("Login Failed!");
    }
});

export const token = router;
开发者ID:vildantursic,项目名称:walter,代码行数:27,代码来源:token.ts


示例4: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
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);
开发者ID:vildantursic,项目名称:walter,代码行数:30,代码来源:complex.ts


示例5: Router

import {Request, Response, Router} from "express";
const router: Router = Router();
import {IRoute} from "express-serve-static-core";
import * as jwt from "jsonwebtoken";
import {key} from "../helpers/key";

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

api.post(async (req: Request, res: Response) => {
    // verify the existing token
    let profile: string = jwt.verify(req.body.token, key);

    // check if the user still exists or if authorization hasn't been revoked
    // if (!valid) return res.send(401); // re-logging

    // issue a new token
    let refreshedToken: string = jwt.sign(profile, key, { expiresIn: "40m" });
    res.json({ token: refreshedToken });
});

export const refreshToken = router;
开发者ID:vildantursic,项目名称:walter,代码行数:21,代码来源:refresh-token.ts


示例6: Router

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

const api: IRoute = router.route("/api/entity/: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 entityModel
        .find({_id: id})
        .exec()
        .catch((e: Error) => {
            res.status(400).send({ error: errorApiMessages.postMessage + e });
        });

    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 entityModel.insertMany(obj);
    res.status(200).json(dataRes);
开发者ID:vildantursic,项目名称:walter,代码行数:31,代码来源:entity.ts


示例7: 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



注:本文中的express-serve-static-core.IRoute类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap