Express REST 服务器从 Ember 数据 PUT 中丢失负载

问题描述 投票:0回答:1

我正在扩展标准 Ember 示例项目(在咖啡脚本中)以与 Express/mongoose RESTful 服务器进行通信。我已使用推荐的方式通过 POSTS 成功获取了所有和单个记录:

Find        GET     /people/123
Find All    GET     /people

现在,当尝试通过 ember-data PUT 更新记录时,通过 Ember 适配器触发。

Update      PUT     /people/123

而且它不起作用。

分析:客户

我使用 Chrome 开发工具分析客户端,使用 console.log 分析服务器端。这是我在客户端看到的内容。 Ember-data 对服务器进行 PUT 和 OPTIONS 调用。

enter image description here

在 PUT 中,我看到有效负载包含用户编辑,并且格式看起来正确。

enter image description here

响应选项卡显示了一堆废话,所以我假设问题出在服务器端。

分析:服务器

在服务器端,当我转储请求变量时,我得到了这个。正文被接收为

{post: {}}
,即结构正确但内容空洞。并收到正确的 mongo id。

enter image description here

这是来自服务器路由器的堆栈跟踪:

Trace
    at /home/vagrant/restl/node_modules/restgen/lib/routes.js:68:15
    at callbacks (/home/vagrant/restl/node_modules/express/lib/router/index.js:161:37)
    at param (/home/vagrant/restl/node_modules/express/lib/router/index.js:135:11)
    at param (/home/vagrant/restl/node_modules/express/lib/router/index.js:132:11)
    at param (/home/vagrant/restl/node_modules/express/lib/router/index.js:132:11)
    at pass (/home/vagrant/restl/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/home/vagrant/restl/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/home/vagrant/restl/node_modules/express/lib/router/index.js:33:10)
    at next (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.session [as handle] (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/session.js:301:7)
    at next (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.cookieParser [as handle] (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.allowCrossDomain [as handle] (/home/vagrant/restl/app.js:21:5)
    at next (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.methodOverride [as handle] (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:49:5)
    at next (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at multipart (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/multipart.js:60:27)
    at /home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9
    at urlencoded (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:48:27)
    at /home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7
    at IncomingMessage.<anonymous> (/home/vagrant/restl/node_modules/express/node_modules/connect/lib/middleware/json.js:82:9)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

我的 Express REST 服务器出了什么问题?

服务器端代码

这里是服务器代码的相关部分,它是从 npm restgen 分叉出来的。

var express = require('express')
  , http = require('http')
  , path = require('path')
  , restgen = require('restgen');

var app = express()
  , mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/rest');

// development only
if ('development' == app.get('env')) {
  app.use(express.logger({ format: '\x1b[1m :date \x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms\x1b[0m :status' }));
}

//CORS middleware
var allowCrossDomain = function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
}
// all environments
app.configure(function() {
  app.set('root', __dirname);
  app.set('port', process.env.PORT || 3000); //3000
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(allowCrossDomain);
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(restgen.ErrorHandler)
});

restgen.Initialize(app, mongoose);

app.use(function(req, res, next){
  next(restgen.RestError.NotFound.insert(req.url));
});

// example of how to throw a 404
app.get('/404', function(req, res, next){
  next(restgen.RestError.NotFound.insert(req.url));
});

// example of how to throw a 500
app.get('/500', function(req, res, next){
  next(new Error('keyboard cat!'));
});

if(!module.parent) {
  http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
  });
}

exports.app = app;

在客户端的某个地方,请求负载丢失了。 我很乐意发布服务器端代码的其他部分,例如路由(如果相关)。请在评论中询问。

客户端代码

这是 Ember 方面,我很确定这是正确的。

# ===== Adapter =====
# Extended to handle mongo's _id as primarykey
App.Adapter = DS.RESTAdapter.extend(
 serializer: DS.RESTSerializer.extend(
  primaryKey: (type) -> "_id"
  )   
)
DS.RESTAdapter.reopen({url: 'http://localhost:3000'});

# ===== Store =====
App.Store = DS.Store.extend(
  revision: 12
  adapter: App.Adapter
)

# ===== Controller =====
module.exports = App.PostController = Ember.ObjectController.extend
    save: ->
        @get("store").commit()
        @get("target.router").transitionTo "posts.index"

# ==== Model ====
module.exports = App.Post = DS.Model.extend
    title: DS.attr 'string'
    author: DS.attr 'string'
    intro: DS.attr 'string'
    extended: DS.attr 'string'
    publishedAt: DS.attr 'date'

更新#1

版本信息来自

npm ls

vagrant@precise32:~/brunch-ember$ npm ls
[email protected] /home/vagrant/brunch-ember
├─┬ [email protected]
│ └─┬ [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   └── [email protected]
├─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
│   └─┬ [email protected]
│     └── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected] extraneous
│ ├── [email protected]
│ └─┬ [email protected]
│   └─┬ [email protected]
│     ├─┬ [email protected]
│     │ └── [email protected]
│     ├── [email protected]
│     ├── [email protected]
│     ├── [email protected]
│     ├── [email protected]
│     └─┬ [email protected]
│       ├── [email protected]
│       ├── [email protected]
│       ├── [email protected]
│       ├─┬ [email protected]
│       │ ├── [email protected]
│       │ └─┬ [email protected]
│       │   └── [email protected]
│       ├─┬ [email protected]
│       │ ├─┬ [email protected]
│       │ │ └── [email protected]
│       │ ├── [email protected]
│       │ ├── [email protected]
│       │ └─┬ [email protected]
│       │   └── [email protected]
│       ├─┬ [email protected]
│       │ ├── [email protected]
│       │ ├── [email protected]
│       │ └── [email protected]
│       ├── [email protected]
│       ├── [email protected]
│       ├── [email protected]
│       ├── [email protected]
│       ├── [email protected]
│       └── [email protected]
├─┬ [email protected] (git+ssh://[email protected]:bartsqueezy/ember-handlebars-brunch.git#19b9cfd141
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   ├─┬ [email protected]
│ │   │ └── [email protected]
│ │   └─┬ [email protected]
│ │     └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   └─┬ [email protected]
│ │     ├── [email protected]
│ │     └── [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├── [email protected]
│ │ │ └── [email protected]
│ │ ├─┬ [email protected]
│ │ │ └── [email protected]
│ │ └─┬ [email protected]
│ │   ├─┬ [email protected]
│ │   │ └── [email protected]
│ │   └─┬ [email protected]
│ │     └── [email protected]
│ └─┬ [email protected]
│   └─┬ [email protected]
│     ├── [email protected]
│     ├─┬ [email protected]
│     │ └── [email protected]
│     └─┬ [email protected]
│       └── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├── [email protected]
│ │ │ ├── [email protected]
│ │ │ └─┬ [email protected]
│ │ │   └── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├─┬ [email protected]
│ │ │ └─┬ [email protected]
│ │ │   ├── [email protected]
│ │ │   └─┬ [email protected]
│ │ │     └── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected] invalid
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └─┬ [email protected]
│ │   ├─┬ [email protected]
│ │   │ └── [email protected]
│ │   ├── [email protected]
│ │   ├─┬ [email protected]
│ │   │ ├── [email protected]
│ │   │ ├── [email protected]
│ │   │ └── [email protected]
│ │   └── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── UNMET DEPENDENCY cli-color 0.2.2
│ ├── UNMET DEPENDENCY commander https://github.com/alexferreira/commander.js/tarball/master
│ ├── UNMET DEPENDENCY ejs latest
│ ├── UNMET DEPENDENCY fleck 0.5.1
│ ├── UNMET DEPENDENCY fs-extra 0.6.1
│ └── UNMET DEPENDENCY rsvp-that-works 1.2.0
├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├─┬ [email protected]
│ │ │ │ └── [email protected]
│ │ │ └─┬ [email protected]
│ │ │   └── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ └─┬ [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   ├─┬ [email protected]
│   │ └── [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   └── [email protected]
├── [email protected]
└─┬ [email protected]
  └─┬ [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    └─┬ [email protected]
      └── [email protected]
node.js express rest mongoose ember-data
1个回答
5
投票

我还没有深入研究,但我认为这可能是 ember-data 和 Restgen 之间的细微差别:restgen 需要表单数据(title=foo&body=bar),除非 url 以“.json”结尾。但 ember 数据正在上传 json,但没有将其添加到 URL 中。

我还没有找到任何一方的任何文档来指示如何更改默认行为,但如果这确实是问题所在,那么您应该能够找到解决方法。

更新:检查这一点的快速方法是右键单击 Chrome 开发人员工具中的 ajax 请求,然后选择“复制为 cURL”,然后将其粘贴到记事本中并编辑 URL 以在末尾包含“.ajax”,然后从命令行运行该命令。如果您的服务器正确处理此问题,那么这是一个好兆头,表明我们走在正确的轨道上。

© www.soinside.com 2019 - 2024. All rights reserved.