Created
February 13, 2018 13:58
-
-
Save matinrco/a795b8a00d1e79df912a3bb4b45edaa4 to your computer and use it in GitHub Desktop.
Create restful API in meteor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| }); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.