Skip to content

Instantly share code, notes, and snippets.

@matinrco
Created February 13, 2018 13:58
Show Gist options
  • Select an option

  • Save matinrco/a795b8a00d1e79df912a3bb4b45edaa4 to your computer and use it in GitHub Desktop.

Select an option

Save matinrco/a795b8a00d1e79df912a3bb4b45edaa4 to your computer and use it in GitHub Desktop.
Create restful API in meteor
import Fiber from 'fibers';
import bodyParser from 'body-parser'
if (Meteor.isServer) {
WebApp.connectHandlers
.use(bodyParser.urlencoded())
.use(bodyParser.json())
.use('/getUserProfile', function(req, res, next) {
// necessary for Collection use and other wrapped methods
Fiber(function() {
const userId = req.body.userId;
const user = Meteor.users.findOne(userId);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(user.profile));
}).run();
});
}
@matinrco
Copy link
Copy Markdown
Author

In the last use(), the first parameter (the path) is optional. If omitted, your handler will be called on every request. In this case, you can do your own matches on req.path or other request properties. If your route is not a match, it is critical to call next() to allow other handlers to process the connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment