本文介绍了无法使用 Node.js Express MongoDB Mongoose CoffeeScript 发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我找到了解决方案,请查看本页底部...

Update: I found the solution, look in the bottom of this page...

我有一个用 CoffeScript 编写的 Node.js、Express、MongoDB、Mongoose 项目,我可以创建和读取数据,但我无法更新.

I've got a Node.js, Express, MongoDB, Mongoose project written in CoffeScript and I can create and read data, but I can't update.

这就是我的代码的样子;

This is what my code looks like;

app.js

 # update
 app.put "/admin/:id.:format?", (req, res) ->
    Content.findById req.body.content.id, (err, c) ->
        c.title = req.body.content.title
        c.body = req.body.content.body
        c.save (err) ->
            switch req.params.format
                when "json"
                    res.send c.__doc
                else
                    res.redirect "/admin"

edit.jade

h2 Edit Content
form(method='post', action='/admin/' + c.id)
  input(name='content[id]', value=c.id, type='hidden')
  input(name='_method', value='PUT', type='hidden')
div
  label Title:
    input(name='content[title]', value=c.title || '')
div
  label Body:
    textarea(name='content[body]')=c.body || ''
div
  input(type='submit', value='Save')

这就是我的控制台所说的

And this is what my console says

127.0.0.1 - - [Thu, 13 Oct 2011 21:39:55 GMT] "POST /admin/4e96ec17fd7da7cb18000001 HTTP/1.1" 404 - "http://localhost:1234/admin/4e96ec17fd7da7cb18000001/edit" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.83 Safari/535.2"

这就是我的浏览器告诉我的

And this is what my browser tells me

Cannot POST /admin/4e96ec17fd7da7cb18000001

推荐答案

我在 http://expressjs 上找到了解决方案.com/guide.html:

"当在表单中使用 PUT 等方法时,我们可以利用一个名为 _method 的隐藏输入,它可以用来改变 HTTP 方法.为此,我们首先需要 methodOverride 中间件,应该放置在 bodyParser 下方,以便它可以利用包含表单值的 req.body."

"When using methods such as PUT with a form, we can utilize a hidden input named _method, which can be used to alter the HTTP method. To do so we first need the methodOverride middleware, which should be placed below bodyParser so that it can utilize it’s req.body containing the form values."

app.use express.bodyParser()
app.use express.methodOverride()

这篇关于无法使用 Node.js Express MongoDB Mongoose CoffeeScript 发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 11:20
查看更多