简体   繁体   English

如何使用 node / express.js 构建多个 post 端点

[英]How to structure a multiple post endpoints with node / express.js

How can I successfully configure my routes to access multiple methods on my controller accordingly to the endpoint and parameters passed to the URL?如何根据传递给 URL 的端点和参数成功配置我的路由以访问 controller 上的多种方法?

When accessing /companies/:companyId/createCheques I would like to call the method createCheques in cheques controller but it is still calling createCheque.访问 /companies/:companyId/createCheques 时,我想在检查 controller 中调用方法 createCheques,但它仍在调用 createCheque。

I tried adding the line below to routes/cheques.js but it did not work.我尝试将下面的行添加到 routes/cheques.js 但它不起作用。

 router.route('/:companyId/createCheques').post(createCheques)

 // routes/companies.js const express = require('express') const { getCompanies, getCompany, deleteCompany, createCompany, updateCompany, } = require('../controllers/companies') // Include other resource routers const userRouter = require('./users') const chequeRouter = require('./cheques') const redeemRouter = require('./redeems') const router = express.Router({ mergeParams: true }) // Re-route into another resources routers router.use('/:companyId/users', userRouter) router.use('/:companyId/cheques', chequeRouter) router.use('/:companyId/createCheques', chequeRouter) router.use('/:companyId/redeems', redeemRouter) router.route('/').get(getCompanies).post(createCompany) router.route('/:id').get(getCompany).put(updateCompany).delete(deleteCompany) module.exports = router;

 // routes/cheques.js const express = require('express') const { getCheques, getCheque, deleteCheque, createCheque, createCheques, updateCheque } = require('../controllers/cheques') // when more than 1 url param is possible to the same route, mergeParams must to be set to true const router = express.Router({ mergeParams: true }) // Advanced results const Cheque = require('../models/Cheque') const advancedResults = require('../middleware/advancedResults') router.route('/').get(advancedResults(Cheque, 'cheques'), getCheques).post(createCheque).post(createCheques) router.route('/:id').get(getCheque).put(updateCheque).delete(deleteCheque) module.exports = router;

The problem you have is that you are defining two POST controllers for the exact same route.您遇到的问题是您正在为完全相同的路线定义两个 POST 控制器。

If you want to call both controllers createCheque and createCheques , when a POST is done to :companyId/createCheques/ , you only need to add next() on the last line of createCheque , just like a middleware.如果您想同时调用两个控制器createChequecreateCheques ,当对:companyId/createCheques/进行 POST 时,您只需在createCheque的最后一行添加next() ,就像中间件一样。 See https://expressjs.com/es/4x/api.html#routerhttps://expressjs.com/es/4x/api.html#router

If you only want to call one controller, then you need to create a separate route for the other controller on your routes/cheques.js file.如果您只想调用一个 controller,那么您需要在您的routes/ cheques.js 文件中为另一个 controller 创建单独的路由。

router
 .route('/cheques') //here the complete path would be companyId/createCheques/cheques
 .post(createCheques)

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

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