[英]Meteor picker server side router use express middleware
I am trying to use express functions like res.send('string')
or res.json(json)
in my meteor rest api using the picker server side router. 我试图使用选择器服务器端路由器在我的meteor rest api中使用
res.send('string')
或res.json(json)
等快速函数。 In the documentation, it says : 在文档中,它说:
You can use existing connect and express middlewares without any issues.
您可以使用现有的连接和表达中间件而不会出现任何问题。
How can I use express funtions like res.send and res.json ? 如何使用res.send和res.json这样的快速功能? When I try to use them, it tells me they are not a function.
当我尝试使用它们时,它告诉我它们不是一个功能。
I have the following main.js file for my server : 我的服务器有以下main.js文件:
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'));
});
And the following route action (routes/helloworld.js) : 以下路由动作(routes / helloworld.js):
function helloworld(params, req, res, next) {
res.send('id:' + params.id);
}
module.exports = helloworld;
I get the following error : 我收到以下错误:
TypeError: res.send is not a function
It yells the same error when I try to use res.json... 当我尝试使用res.json时,它会大喊同样的错误...
packages.json : packages.json:
{
"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 I found that I can mimic res.json with this code : 更新我发现我可以用这段代码模仿res.json:
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;
As Picker/Meteor is not using express, it does not have res.send() and res.json(). 由于Picker / Meteor没有使用express,它没有res.send()和res.json()。
However, you can explore Restivus, which is a high level api wrapper which handle json automatically. 但是,您可以探索Restivus,这是一个自动处理json的高级api包装器。
http://meteorpedia.com/read/REST_API#Restivus http://meteorpedia.com/read/REST_API#Restivus
Following is the sample code from above link: 以下是上面链接的示例代码:
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"}
};
}
}
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.