1const request = require('request-promise');
2
3module.exports = (app) => {
4
5 // you'll need to have requested 'user_about_me' permissions
6 // in order to get 'quotes' and 'about' fields from search
7 const userFieldSet = 'name, link, is_verified, picture';
8 const pageFieldSet = 'name, category, link, picture, is_verified';
9
10
11 app.post('/facebook-search', (req, res) => {
12 const { queryTerm, searchType } = req.body;
13
14 const options = {
15 method: 'GET',
16 uri: 'https://graph.facebook.com/search',
17 qs: {
18 access_token: config.user_access_token,
19 q: queryTerm,
20 type: searchType,
21 fields: searchType === 'page' ? pageFieldSet : userFieldSet
22 }
23 };
24
25 request(options)
26 .then(fbRes => {
27// Search results are in the data property of the response.
28// There is another property that allows for pagination of results.
29// Pagination will not be covered in this post,
30// so we only need the data property of the parsed response.
31 const parsedRes = JSON.parse(fbRes).data;
32 res.json(parsedRes);
33 })
34 });
35}
36
1app.get('/facebook-search/:id', (req, res) => {
2
3 // you need permission for most of these fields
4 const userFieldSet = 'id, name, about, email, accounts, link, is_verified, significant_other, relationship_status, website, picture, photos, feed';
5
6 const options = {
7 method: 'GET',
8 uri: `https://graph.facebook.com/v2.8/${req.params.id}`,
9 qs: {
10 access_token: user_access_token,
11 fields: userFieldSet
12 }
13 };
14 request(options)
15 .then(fbRes => {
16 res.json(fbRes);
17 })
18})
19