在Express.js中使用PUT方法 [英] Using the PUT method with Express.js

查看:860
本文介绍了在Express.js中使用PUT方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Express.js应用程序实现更新功能,我想使用PUT请求发送新数据,但我一直在使用PUT收到错误。从我读过的所有内容来看,这只是使用app.put的问题,但这不起作用。我的路线文件中有以下内容:

I'm trying to implement update functionality to an Express.js app, and I'd like to use a PUT request to send the new data, but I keep getting errors using PUT. From everything I've read, it's just a matter of using app.put, but that isn't working. I've got the following in my routes file:

send = function(req, res) { 
    req.send(res.locals.content);
};

app.put('/api/:company', function(res,req) {
    res.send('this is an update');
}, send);

当我使用邮递员发出PUT请求时,我得到一个不能PUT / api / petshop作为一个错误。我不明白为什么我不能PUT,或者出了什么问题。

When I use postman to make a PUT request, I get a "cannot PUT /api/petshop" as an error. I don't understand why I can't PUT, or what's going wrong.

推荐答案

你可能缺乏实际的更新功能。当你告诉数据库更新数据时,你有put路径将结果返回给客户端但缺少部分。

You may be lacking the actual update function. You have the put path returning the result back to the client but missing the part when you tell the database to update the data.

如果你使用的是mongodb和express,你可以这样写:

If you're using mongodb and express, you could write something like:

app.put('/api/:company', function (req, res) {
    var company = req.company;

    company = _.extend(company, req.body);

    company.save(function(err) {
    if (err) {
        return res.send('/company', {
            errors: err.errors,
            company: company
        });
    } else {
        res.jsonp(company);
    }

}); 

这个平均堆栈项目可以帮助您,因为它涵盖了这个CRUD功能我刚刚在这里用你的公司交换他们的文章。同样的。

This mean stack project may help you as it covers this CRUD functionality which I just used here swapping their articles for your companies. same same.

这篇关于在Express.js中使用PUT方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆