在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:CollectionFS/Meteor-http-methods开源软件地址:https://github.com/CollectionFS/Meteor-http-methods开源编程语言:JavaScript 100.0%开源软件介绍:cfs:http-methods
If you're looking for an alternative, we highly recommend Meteor-Files by VeliovGroup Add server-side methods to the UsageThe HTTP.methods({
'list': function() {
return '<b>Default content type is text/html</b>';
}
}); Methods scopeThe methods scope contains different kinds of inputs. We can also get user details if logged in.
Passing data via headerFrom the client: HTTP.post('list', {
data: { foo: 'bar' }
}, function(err, result) {
console.log('Content: ' + result.content + ' === "Hello"');
}); HTTP Server method: HTTP.methods({
'list': function(data) {
if (data.foo === 'bar') {
/* data we pass via the header is parsed by EJSON.parse
If not able, then it returns the raw data instead */
}
return 'Hello';
}
}); ParametersThe method name or URL can be used to pass parameters to the method. The parameters are available on the server under Client HTTP.post('/items/12/emails/5', function(err, result) {
console.log('Got back: ' + result.content);
}); Server HTTP.methods({
'/items/:itemId/emails/:emailId': function() {
// this.params.itemId === '12'
// this.params.emailId === '5'
}
}); Extended usageThe Example: HTTP.methods({
'/hello': {
get: function(data) {},
// post: function(data) {},
// put: function(data) {},
// delete: function(data) {},
// options: function() {
// // Example of a simple options function
// this.setStatusCode(200);
// this.addHeader('Accept', 'POST,PUT');
// // The options for this restpoint
// var options = {
// POST: {
// description: 'Create an issue',
// parameters: {
// title: {
// type: 'string',
// description: 'Issue title'
// }
// }
// }
// };
// // Print the options in pretty json
// return JSON.stringify(options, null, '\t');
// },
// stream: true // flag whether to allow stream handling in the request
}
}); In this example the mounted http rest point will only support the Example: HTTP.methods({
'/hello': {
method: function(data) {},
}
}); In this example all methods AuthenticationThe client needs the Client HTTP.post('/hello', {
params: {
token: Accounts && Accounts._storedLoginToken()
}
}, function(err, result) {
console.log('Got back: ' + result.content);
}); Server 'hello': function(data) {
if (this.userId) {
var user = Meteor.users.findOne({ _id: this.userId });
return 'Hello ' + (user && user.username || user && user.emails[0].address || 'user');
} else {
this.setStatusCode(401); // Unauthorized
}
} Using custom authenticationIt's possible to make your own function to set the userId - not using the built-in token pattern. // My auth will return the userId
var myAuth = function() {
// Read the token from '/hello?token=5'
var userToken = self.query.token;
// Check the userToken before adding it to the db query
// Set the this.userId
if (userToken) {
var user = Meteor.users.findOne({ 'services.resume.loginTokens.token': userToken });
// Set the userId in the scope
return user && user._id;
}
};
HTTP.methods({
'/hello': {
auth: myAuth,
method: function(data) {
// this.userId is set by myAuth
if (this.userId) { /**/ } else { /**/ }
}
}
}); The above resembles the builtin auth handler SecurityWhen buffering data instead of streaming we set the buffer limit to 5mb - This can be changed on the fly: // Set the max data length
// 5mb = 5 * 1024 * 1024 = 5242880;
HTTP.methodsMaxDataLength = 5242880; Login and logout (TODO)These operations are not currently supported for off Meteor use - there are some security considerations.
HTTP Client-side usageIf you want to use the HTTP client-side functionality and find yourself having a hard time viewing all available options; these can be found on https://docs.meteor.com/#/full/http. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论