PUT和DELETE始终在Node + Express中路由到GET

时间:2014-10-11 21:58:05

标签: node.js express

我是Node / Express的初学者。我试图制作一个CRUD应用程序,但坚持更新和删除。我认为我的路由器代码有问题,但我不知道为什么。以下代码在我的控制器中,一切正常,但PUTDELETE。它总是路由到GET。我尝试使用next();,但它返回此错误:Can't set headers after they are sent.

我可以使用GET /:company_id/delete进行删除工作,但这不是一个好的标准化解决方案。如何使更新和删除过程有效?

'use strict';

var Companies = require('../../models/companies');

module.exports = function (router) {

  // INDEX
  // accessed at GET http://localhost:8000/companies
  router.get('/', function (req, res) {

    Companies.find(function(err, model) {

      if (err) {
        res.send(err);
      }
      else {
        res.format({
          json: function () {
            res.json(model);
          },
          html: function () {
            res.render('companies/index', model);
          }
        });
      }

    });

  });

  // CREATE VIEW
  // accessed at GET http://localhost:8000/companies/create
  router.get('/create', function (req, res) {

      res.render('companies/create');

  });

  // CREATE DATA
  // accessed at POST http://localhost:8000/companies
  router.post('/', function (req, res) {

    var name = req.body.name && req.body.name.trim();
    var type = req.body.type && req.body.type.trim();

    // VALIDATION
    if (name === '') {
      res.redirect('/companies/create');
      return;
    }

    var model = new Companies({name: name, type: type});

    model.save(function(err) {
      if (err) {
        res.send(err);
      }
      else {
        res.redirect('/companies');
      }
    });

  });

  // READ
  // accessed at GET http://localhost:8000/companies/:company_id
  router.get('/:company_id', function(req, res) {

    Companies.findById(req.params.company_id, function(err, model) {

      if (err) {
        res.send(err);
      }
      else {
        res.render('companies/read', model);
      }

    });

  });

  // UPDATE VIEW
  // accessed at GET http://localhost:8000/companies/:company_id/edit
  router.get('/:company_id/edit', function(req, res) {

    Companies.findById(req.params.company_id, function(err, model) {

      if (err) {
        res.send(err);
      }
      else {
        res.render('companies/edit', model);
      }

    });

  });

  // UPDATE DATA
  // accessed at PUT http://localhost:8000/companies/:company_id
  router.put('/:company_id', function(req, res) {

    Companies.findById(req.params.company_id, function(err, model) {

      if (err) {
        res.send(err);
      }
      else {
        model.name = req.body.name;
        model.type = req.body.type;

        model.save(function(err) {
          if (err) {
            res.send(err);
          }
          else {
            res.redirect('/companies');
          }
        });

      }
    });

  });

  // DELETE
  // accessed at DELETE http://localhost:8000/companies/:company_id
  router.delete('/:company_id', function (req, res) {

    Companies.remove({ _id: req.params.company_id }, function(err) {

      if (err) {
        res.send(err);
      }
      else {
        res.redirect('/companies');
      }

    });

  });

};

1 个答案:

答案 0 :(得分:2)

HTML表单仅支持GETPOST。 XMLHTTPRequest支持PUTDELETE,因此您可能必须使用该路由或使用method-override之类的内容来允许使用其他HTTP谓词提交HTML表单。