I need to build an API. e.g. calling localhost:3000/api/param should give me the parameter param and should output an JSON string. I don’t know how to get this done. So I thought to use a route (I’m using flowrouter), so I created something like this:
This gives my the object {type: 'param'} in the console.
But I need to output a JSON string instead an output in the console. Also there is also rendered partial HTML-content of the application, but in this case I need to get a JSON string as output.
How do I do that? Maybe this is a complete wrong way…
If you’re starting from scratch and/or going to be doing a lot with the API, it may be helpful to have a look at Apollo, which is considered by many to have great advantages vs. REST.
Thank you. Now I am using Picker and it is working very easily. The only thing is, that I need to authentificate a user. So how do I pass login data and check this on server side in a meteor way?
restivus is not working for me as I have to filter a collection. Can’t use getAll or a single document, but need to get some documents from the collection…
import { Picker } from 'meteor/meteorhacks:picker';
import bodyParser from 'body-parser';
Picker.middleware(bodyParser.urlencoded({ extended: false }));
Picker.middleware(bodyParser.json());
// Now you're ready to use Picker as documented, and it will handle JSON data
Look into the simple:rest package for authentication, it allows you to receive a meteor token on login (which you can save in local storage) and then the request can authenticate the user by looking up the user by their token. simple:rest also has a lot of nice things to replace picker… it’s way more polished IMHO.
Diden’t have much luck with Picker when I tried to parse a form previously. Take a look at https://docs.meteor.com/packages/webapp.html works much better for me and not outdated as Picker.