本文整理汇总了TypeScript中express.Router.put方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Router.put方法的具体用法?TypeScript Router.put怎么用?TypeScript Router.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类express.Router
的用法示例。
在下文中一共展示了Router.put方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createRoutes
export function createRoutes(router: Router, auth: RequestHandler, setRouteVar: RequestHandler): void {
// list route
router.get('/', setRouteVar, list)
// single entry route
router.get('/:slug', setRouteVar, get)
// comment route
router.get('/:slug/comments', setRouteVar, comments)
// action list route
router.get('/:slug/:sublist', setRouteVar, sublist)
// check if slug available
router.post('/unique', setRouteVar, isUnique)
// create route
router.post('/', auth, setRouteVar, create)
// create gallery
router.put('/:slug/gallery', auth, setRouteVar, upload)
// clear gallery
router.delete('/:slug/gallery', auth, setRouteVar, clear, update)
// update route
router.patch('/:slug', auth, setRouteVar, update)
// delete route
router.delete('/:slug', auth, setRouteVar, remove)
}
示例2: init
public init() {
this.router.get('/boards', function (req, res) {
board.find({}, function (err, boards) {
res.json(boards);
});
});
this.router.post('/newBoard', function (req, res) {
let newBoard = new board(req.body);
newBoard.save(function (err) {
if (err) {
res.send(err);
}
res.json({
success: true,
message: 'Board created successfully',
});
});
});
this.router.delete('/delete/:id', function (req, res) {
board.findById(req.params.id, (err, responseByID) => {
responseByID.remove(req.body, (err) => {
if (err) {
res.status(500).send(err);
}
board.find((err) => {
if (err) {
res.status(500).send(err)
}
res.json({
success: true,
message: 'Removed!',
});
});
});
});
});
this.router.put('/update/:id', function (req, res) {
board.findById(req.params.id, (err, responseByID) => {
responseByID.update(req.body, (err) => {
if (err) {
res.status(500).send(err);
}
board.find((err) => {
if (err) {
res.status(500).send(err)
}
res.json({
success: true,
message: 'Board update successfully'
});
});
});
});
});
}
示例3: mount
mount(router: Router) {
var controller = require(this.controller)
// if there is a target method, check to see if it exists and replace the original controller with it
if (this.target) {
if (!controller.hasOwnProperty(this.target)) {
throw new Error("target function " + this.target + " does not exist on controller " + this.controller)
}
controller = controller[this.target]
}
// if controller is not a function, throw an error
if (typeof (controller) != "function") {
throw new Error("controllers must resolve to a function")
}
// use a switch (rather than checking for the method) to prevent
// unwanted method calls (ie. router.route(this.path, controller) would not be good...)
switch (this.method.toLowerCase()) {
case "all":
router.all(this.path, controller)
break
case "get":
router.get(this.path, controller)
break
case "put":
router.put(this.path, controller)
break
case "post":
router.post(this.path, controller)
break
case "delete":
router.delete(this.path, controller)
break
case "patch":
router.patch(this.path, controller)
break
case "use":
if (typeof this.path === "string") {
router.use(this.path, controller);
} else {
router.use(controller);
}
break;
case "param":
if (typeof this.path === "string") {
router.param(this.path, controller);
} else {
router.param(controller);
}
break;
default:
throw new Error("unsupported method " + this.method)
}
}
示例4: createRoutes
export function createRoutes(router: Router, setRouteVar: RequestHandler, auth: RequestHandler): void {
// list route
router.get('/users', setRouteVar, list)
// route to check unique values
router.post('/users/unique', setRouteVar, isUnique)
// create route
router.post('/users', setRouteVar, create)
// TOTP login route - create
router.post('/login/totp', setRouteVar, initTotp, sendTotp)
// TOTP login route - login/signup
router.patch('/login/totp', setRouteVar, verifyTotp, login)
// reset password routes via TOTP
router.patch('/login/reset', setRouteVar, verifyPasswords, verifyTotp)
// update routes via TOTP
router.patch('/self/totp', setRouteVar, auth, verifyTotp)
// login routes via username/passport
router.post('/login/local', setRouteVar, local, login)
// JWT login routes
router.get('/self', auth, login)
// sublist route
router.get('/self/:sublist', auth, sublist)
// user created content route
router.get('/self/:sublist/:slug', auth, content)
// update route
router.patch('/self', auth, update)
// update user avatar
router.put('/self/avatar', setRouteVar, auth, avatar)
// delete route
router.delete('/self', auth, remove)
// user submit content route
router.post('/self/submit', auth, submit)
// user retract content route
router.post('/self/retract', auth, retract)
}
示例5: Router
export const info = ({ db }: { db: any }) => {
const api: Router = Router();
/**
* Use this endpoint to fetch how many tokens have been sold
*/
api.get('/tokens-sold', async (req: Request, res: Response) => {
try {
const usdTotals: Array<UsdTotal> = await findAll<UsdTotal>(DB_USD_TOTALS);
if (req.query.currency === 'usd' && usdTotals.length > 0) {
return res.send({amount: precisionRound(usdTotals[0].currentRaised, 2), milestone: usdTotals[0].nextMilestone });
}
res.status(409).json({ message: 'object or parameter not found.'});
} catch (err) {
res.status(409).json(err);
}
});
/**
* This endpoint manually updates the amount raised in USD. Because there are
* so many influencing factors on tokens coming in and out we manually calculate
* revenue progress and update it
*/
api.put('/update-usd', async (req: Request, res: Response) => {
try {
const totals: Array<UsdTotal> = await findAll<UsdTotal>(DB_USD_TOTALS);
if (totals.length === 0) {
const total: UsdTotal = {
_id: new ObjectID(),
currentRaised: req.body.currentRaised,
nextMilestone: req.body.nextMilestone
};
await saveWithId<UsdTotal>(DB_USD_TOTALS, total);
} else {
totals[0].currentRaised = precisionRound(req.body.currentRaised, 2);
totals[0].nextMilestone = precisionRound(req.body.nextMilestone, 2);
const id = totals[0]._id.toHexString();
delete totals[0]._id;
await update<UsdTotal>(DB_USD_TOTALS, id, totals[0]);
}
return res.status(202).json({message: 'ok'});
} catch (err) {
res.status(409).json(err);
}
});
return api;
};
示例6: Router
export const kyc = ({ db }: { db: any }) => {
const api: Router = Router();
/**
* We never store KYC in a database - it is forwarded into a gmail account
*/
api.put('/email', vKYCEmail, async (req: Request, res: Response) => {
try {
if (!(isImage(req.body[KEY_LEGAL_PHOTO_ID_PATH].path)) && req.body[KEY_PERSONAL_PICTURE_HOLDING_ID_PATH].path && req.body[KEY_PROOF_OF_RESIDENCE_PATH].path) {
return res.status(409).json(new Error('Files must be jpg, png or tiff.'));
}
await sendKYCEmail(kycTemplate(req));
return res.status(202).json({message: 'ok'});
} catch (err) {
res.status(409).json(err);
}
});
return api;
};
示例7: registerRoute
function registerRoute(route: RouteRegistration, router: Router, routeUrl: string) {
switch (route.method) {
case RouteMethod.Get:
router.get(routeUrl, (route.descriptor.value as any));
break;
case RouteMethod.Put:
router.put(routeUrl, (route.descriptor.value as any));
break;
case RouteMethod.Post:
router.post(routeUrl, (route.descriptor.value as any));
break;
case RouteMethod.Delete:
router.delete(routeUrl, (route.descriptor.value as any));
break;
case RouteMethod.Head:
router.head(routeUrl, (route.descriptor.value as any));
break;
default:
throw new HttpVerbNotSupportedError(route.method);
}
}
示例8: require
import { Router } from 'express';
var authenticatedPolicy = require("../../policies/authenticated");
import { CollaboratorController } from "../../../app/controllers/collaborator/collaborator.controller";
export const COLLABORATOR_MAIN_ROUTER: Router = Router();
// define routes
//create
COLLABORATOR_MAIN_ROUTER.post('/', CollaboratorController.create);
//getById
COLLABORATOR_MAIN_ROUTER.get('/:id', authenticatedPolicy, CollaboratorController.getById);
//getAll
COLLABORATOR_MAIN_ROUTER.post('/getAll', CollaboratorController.getAll);
//update
COLLABORATOR_MAIN_ROUTER.put('/:id', authenticatedPolicy, CollaboratorController.updateById);
//delete
COLLABORATOR_MAIN_ROUTER.delete('/:id', authenticatedPolicy, CollaboratorController.deleteById);
示例9:
})
})
UsersRouter.get("/:id", (request: Request, response: Response, next: NextFunction) => {
User.findById(request.params.id, (err: any, user: IUser) => {
if (err) return next(err)
response.json(user)
})
})
UsersRouter.put("/:id", (request: Request, response: Response, next: NextFunction) => {
User.findById(request.params.id, (err: any, user: IUser) => {
if (err) return next(err)
request.body.forEach((property: any) => {
user[property] = request.body[property]
});
user.save((err: any, user: IUser) => {
if (err) return next(err)
response.json(user)
})
})
})
UsersRouter.delete("/:id", (request: Request, response: Response, next: NextFunction) => {
User.findByIdAndRemove(request.params.id, (err: any, user: IUser) => {
if (err) return next(err)
response.json({})
})
})
export { UsersRouter }
示例10: getIdeas
let registerIdeaRoutes = (
router: Router,
ideaService: IdeaService,
skillService: SkillService,
tagService: TagService,
techService: TechnologyService) => {
router.get('/ideas', getIdeas);
router.get('/ideas/:id', getIdeaById);
router.post('/ideas', createIdea);
router.put('/ideas/:id', updateIdea);
router.delete('/ideas/:id', deleteIdea)
function getIdeas(req: any, res: any) {
let searchStr = req.query.search;
if (searchStr) {
ideaService.search(searchStr)
.then((results: any) => {
res.json(results || []);
})
.catch((err: any) => {
res.status(500).send(err).end();
});
} else {
ideaService.getMany()
.then((ideas: any) => {
res.json(ideas || []);
})
.catch((err: any) => {
res.status(500).send(err).end();
});
}
}
function getIdeaById(req: any, res: any) {
ideaService.getById(req.params.id)
.then((idea: any) => {
if (idea) {
res.json(idea);
} else {
notFoundSvc.send404(req, res);
}
})
.catch((err: any) => {
res.status(500).send(err).end();
});
}
function createIdea(req: any, res: any) {
//TODO: add location header
ideaService.insert(req.body)
.then((idea: any) => {
if (idea) {
for (let tag of idea.tags) {
tagService.save(tag);
}
for (let skill of idea.skills) {
skillService.save(skill);
}
for (let tech of idea.technologies) {
techService.save(tech);
}
res.status(201).json(idea);
} else {
res.status(500).send('unknown error').end();
}
})
.catch((err: any) => {
res.status(500).send(err).end();
});
}
function updateIdea(req: any, res: any) {
let idea = req.body;
if (req.params.id !== idea._id) {
res.status(400).send('id in payload does not match id in url').end();
}
ideaService.update(idea)
.then((result: any) => {
if (result === true) {
for (let tag of idea.tags) {
tagService.save(tag);
}
for (let skill of idea.skills) {
skillService.save(skill);
}
for (let tech of idea.technologies) {
techService.save(tech);
}
res.status(204).end();
} else {
res.status(500).send('unknown error').end();
}
})
.catch((err: any) => {
res.status(500).send(err).end();
});
}
function deleteIdea(req: any, res: any) {
//.........这里部分代码省略.........