GET路由上的Restify和Backbone冲突

时间:2013-08-01 18:09:38

标签: node.js backbone.js restify

我在使用带有主干和restify的PushState时遇到了困难。我的大多数路线工作正常,但单个模型的GET路线存在冲突。我已经解决了这个问题,只有那些有XMLHttpRequest标题集的人才能解决问题。

server.js

var app = restify.createServer();
app.use(restify.bodyParser());

// serve static files
app.get(/^\/public\/(.*)/, public.serveFile);

// Read and handle 'post' model
app.get('/posts/:id', function (req, resp, next) {

    // process backbone requests
    if (req.headers['x-requested-with'] === 'XMLHttpRequest') {
        posts.single(req, resp, next);
    } else {
        index(req, resp, next);
    }

});

// a catch all that gives backbone control
app.get(/.*/, index);

骨干路由器

// pushstate is true
routes: {
    '': 'home',
    'posts/:id': 'show', // issues
    '*other': 'default'
},

我可以让骨干处理/posts/上的所有请求,然后创建一个单独的api路由来处理基本的CRUD操作,但是如果可能的话我想保留一个奇偶校验。

我当前的解决方案有效,但看起来很不稳定,有更好的方法来解决这个问题吗?

0 个答案:

没有答案