本文整理匯總了TypeScript中express-serve-static-core.IRoute.post方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript IRoute.post方法的具體用法?TypeScript IRoute.post怎麽用?TypeScript IRoute.post使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類express-serve-static-core.IRoute
的用法示例。
在下文中一共展示了IRoute.post方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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;
示例2: 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;
示例3: checkIfDataIsArray
}
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);
});
api.put(async (req: Request, res: Response) => {
if (!checkObjectIDValidity(req.params.id)) {
res.status(400).json(errorIDValidationMessages.putMessage);
}
let obj: Object = await complexModel
.update({ _id: req.params.id }, { $set: req.body })
.lean()
.exec()
.catch((e: Error) => {
res.status(400).send({ error: errorApiMessages.putMessage + e });
示例4: checkIfDataIsArray
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);
});
api.put(async (req: Request, res: Response) => {
if (!checkObjectIDValidity(req.params.id)) {
res.status(400).json(errorIDValidationMessages.putMessage);
}
let obj: Object = await entityModel
.update({ _id: req.params.id }, { $set: req.body })
.lean()
.exec()
.catch((e: Error) => {
res.status(400).send({ error: errorApiMessages.putMessage + e });