我有以下代码,该代码对于GET和POST正常工作,但对于节点js中的PUT则工作不正常。我正在对GET和PUT使用方法重载。下面是我的app.js代码。
如果我在这里遗漏了什么,可以请帮我。基本上,我试图通过在URL的客户名以及请求的BODY部分中的其他详细信息来进行PUT请求。因此,如果存在具有名称的客户,它将对其进行更新,否则将重新创建。
import { Router } from 'express';
const AuthRoutes = Router();
const AppRoutes = Router();
AuthRoutes.get('/customer/:customername', getCustomerbyname);
AuthRoutes.post('/calculate/:id', calculate);
AuthRoutes.put('/customer/:customername', saveCustomer);
在URL下方,我正在尝试PUT。
我不知道我在这里想念什么。
错误-您无权访问/ domainname / customer / abc
答案 0 :(得分:1)
// routes.js
import { Router } from 'express';
const Router = Router()
Router.get('/customer/:customername', getCustomerbyname)
.put('/customer/:customername', saveCustomer);
Router.post('/calculate/:id', calculate);
module.exports = router
/// in app.js or server.js or index.js
// import the router
import {router as customerRoutes} from './routes.js
// add to your app
app.use('/',customerRoutes)
答案 1 :(得分:0)
好像您已经为路由进行了身份验证,但是忘记了对它进行身份验证? Middleware
可能已设置,因此请尝试进行身份验证。