首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >流星拾取服务器端路由器使用快速中间件

流星拾取服务器端路由器使用快速中间件
EN

Stack Overflow用户
提问于 2016-08-29 02:57:51
回答 1查看 1.6K关注 0票数 6

我正在使用res.send('string')服务器端路由器在meteor中使用像采摘机res.json(json)这样的快速函数。在文件中,它说:

您可以使用现有的连接和表示中间件,没有任何问题。

如何使用像res.send和res.json这样的快速功能?当我尝试使用它们时,它告诉我它们不是一个函数。

我的服务器有以下main.js文件:

代码语言:javascript
复制
import { Meteor } from 'meteor/meteor';
import { Picker} from 'meteor/meteorhacks:picker';

var bodyParser = Meteor.npmRequire('body-parser'),
    methodOverride = Meteor.npmRequire('method-override'),
    logger = Meteor.npmRequire('morgan');

Picker.middleware(bodyParser.json());
Picker.middleware(bodyParser.urlencoded({extended:false}));
Picker.middleware(logger('dev'));
Picker.middleware(methodOverride('X-HTTP-Method'));          // Microsoft
Picker.middleware(methodOverride('X-HTTP-Method-Override')); // Google/GData
Picker.middleware(methodOverride('X-Method-Override'));

Meteor.startup(() => {
    console.log('meteor server started');

    var postRoutes = Picker.filter(function(req, res) {
        return req.method == "POST";
    });

    postRoutes.route('/post/:id', require('./routes/helloworld'));
});

以及以下路由操作( route /helloworld.js):

代码语言:javascript
复制
function helloworld(params, req, res, next) {
    res.send('id:' + params.id); 
}

module.exports = helloworld;

我得到以下错误:

代码语言:javascript
复制
TypeError: res.send is not a function

当我试图使用res.json时,它会发出同样的错误.

packages.json:

代码语言:javascript
复制
{
    "body-parser": "1.15.2",
    "chai": "3.5.0",
    "chai-http": "3.0.0",
    "method-override": "2.3.6",
    "mocha": "3.0.2",
    "moment": "2.14.1",
    "moment-timezone": "0.5.5",
    "morgan": "1.7.0",
    "supertest": "2.0.0",
    "supertest-as-promised":"4.0.0",
    "express":"4.14.0"
}

UPDATE我发现我可以用以下代码模拟res.json:

代码语言:javascript
复制
function helloworld(params, req, res, next) {
    console.log(req.body);
    res.setHeader( 'Content-Type', 'application/json' );
    res.end( JSON.stringify({id:params.id}) );
}

module.exports = helloworld;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-04 03:07:27

由于Picker/Meteor没有使用express,所以它没有res.send()和res.json()。

但是,您可以探索Restivus,它是一个高级api包装器,可以自动处理json。

API#Restivus

以下是上述链接中的示例代码:

代码语言:javascript
复制
if(Meteor.isServer) {
  Meteor.startup(function () {
    // Global configuration
    Api = new Restivus({
      version: 'v1',
      useDefaultAuth: true,
      prettyJson: true
    });

    // Generates: GET/POST on /api/v1/users, and GET/PUT/DELETE on /api/v1/users/:id 
    // for Meteor.users collection (works on any Mongo collection)
    Api.addCollection(Meteor.users);
    // That's it! Many more options are available if needed...

    // Maps to: POST /api/v1/articles/:id
    Api.addRoute('articles/:id', {authRequired: true}, {
      post: {
        roleRequired: ['author', 'admin'],
        action: function () {
          var article = Articles.findOne(this.urlParams.id);
          if (article) {
            return {status: "success", data: article};
          }
          return {
            statusCode: 400,
            body: {status: "fail", message: "Unable to add article"}
          };
        }
      }
    });
  });
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39197919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档