【问题标题】:How to perform PUT in Node JS如何在 Node JS 中执行 PUT
【发布时间】:2020-03-04 16:49:41
【问题描述】:

下面是我的 app.js 代码。

import bodyParser from 'body-parser';
import cors from 'cors';
import requestIp from 'request-ip';
import os from 'os';
import { AppRoutes, AuthRoutes } from './routes';


const app = express();
app.use(cors());
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next)=> {
  const clientIp = requestIp.getClientIp(req);
  logger.debug(JSON.stringify(req.socket.address()));
  logger.debug(`incoming IP ${clientIp}`);
  next();
});
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
     next();
});

// Api Routes.
app.use('/api/login', AppRoutes);
app.use('/api', verifyToken, AuthRoutes);


export default app;

下面是我的 index.js 代码。下面的代码适用于 GET 和 POST,但不适用于 PUT。它给出了一个错误。

您无权访问 /api/save-user-profile/{user-name}。

import {
  getCustomersbyId
} from './controller/customer-controller';
import { Login } from './controller/login';
import {
  modelEdit,
  saveProfile
} from './controller/extension';

const AuthRoutes = Router();
const AppRoutes = Router();

AuthRoutes.get('/customers/', getCustomersbyId);
AuthRoutes.post('/model-entity-links/info', modelEdit);
AuthRoutes.put('/save-user-profile/:username', saveProfile);
AppRoutes.post('/', Login);

export { AuthRoutes, AppRoutes };

当我将 PUT 更改为 POST 时,它工作正常。但我想用 PUT 来做。我在这里缺少任何配置 PUT 的东西吗? 我在 app.js 中也尝试过下面的代码。但仍然遇到同样的问题。

       res.header("Access-Control-Allow-Origin", "*");
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
       res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

我的 saveProile 代码如下。

export const saveUserProfile = (req, res) => {
  logger.debug({ method: 'saveUserProfile', message: MESSAGE.ENTER });
  const { username } = req.params;
  const userProfileInfo = JSON.parse(JSON.stringify(req.body));
  if (username &&  !isEmptyObject(userProfileInfo)) {
    postUserProfile(username, userProfileInfo)
    .then(r=>{
      res.status(200).send(r.data);
    })
    .catch(e=>{
      logger.error({ method: 'saveUserProfile', message: e.response ? JSON.stringify(e.response.data.response_message) : e });
      parseError(e, res);
    });
  } else {
    logger.error({ method: 'saveUserProfile', message: MESSAGE.ERROR_INVALID_PARAM });
    parseError(MESSAGE.ERROR_INVALID_PARAM, res, true);
  }
};


【问题讨论】:

  • 你能分享你的 saveProfile 模块以及你在尝试使用 put 时遇到的错误吗
  • 我添加了 saveProfile 代码是个问题。但它无法达到这一点。出现错误 - “您无权访问 /api/save-user-profile/{user-name}。”
  • 您使用的是哪个数据库以及您是如何编写查询的
  • 我正在使用 oracle 数据库。
  • 我可以在本地完成,但是一旦我部署到服务器它就不能工作了。

标签: node.js rest api express


【解决方案1】:

我已经解决了这个问题。这是一个 apache 服务器问题,其中 PUT 和 DELETE 操作受到限制。我们已经对 apache 配置进行了更改,并且它起作用了。谢谢大家的回复。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多