Nodejs程式

var express = require('express');
var bodyParser = require('body-parser')
var app = express();
var FB = require('fb');
var request = require('request');
var fb = new FB.Facebook({ version: 'v2.4' });

app.use(bodyParser.json());// 解析application/json
app.use(bodyParser.urlencoded({ extended: true }));//解析從網頁送來的資料
app.use(express.static('public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

//========================================================

app.get('/', function (req, res) {
    console.log("GET /");
    res.sendFile(__dirname + '/views/facebook.html');
});

app.get('/fb/:fbid', function (req, res) {

    var posts;

    //先取得粉絲團資料
    FB.api(req.params.fbid + '?fields=picture,name', function (response) {

        if (!response || response.error) {
            if (response.error.code == 104) {
                getTokenAndSet(function () {
                    res.redirect(req.originalUrl);
                });
            } else {
                console.log(!response ? 'error occurred' : response.error);
                res.send(response.error);
            }
            return;
        }

        var name = response.name;
        var img = response.picture.data.url
        //================================================
        FB.api(req.params.fbid + '/posts?fields=comments.summary(true),likes.summary(true),created_time,shares', function (response) {

            posts = response.data;

            getNextPagesPosts(0,response.paging.next, posts, function (posts) {
                res.send({ info: {name:name,img:img}, data: posts });
            });

        });
        //================================================
    });

});


function getNextPagesPosts(page,url, posts, callback) {
    request(url, posts, function (err, resp, body) {
        if (!err && resp.statusCode == 200) {
            var result = JSON.parse(body);
            var nextPageUrl = result.paging.next || null;
            // console.log(nextPageUrl?'YES':'NO');
            process.stdout.write("get FB data from page "+page+"\r");

            posts = posts.concat(result.data);
            if (nextPageUrl) {
                getNextPagesPosts(page+1,nextPageUrl, posts, callback);
            } else {
                process.stdout.write("get all FB data           \r");
                callback(posts);
            }
        }
    });

}




app.get('/index', function (req, res) {
    res.render('index');

});

app.listen(3000, function () {
    console.log("伺服器跑起來啦!在你的瀏覽器網址列輸入 localhost:3000吧!");
});


var getTokenAndSet = function (cb) {

    FB.api('oauth/access_token', {
        client_id: '258153304592229',
        client_secret: 'cce3196058cee00a8b486bca55519ef7',
        grant_type: 'client_credentials'
    }, function (response) {
        if (!response || response.error) {
            console.log(!response ? 'error occurred' : response.error);
            return;
        }

        var accessToken = response.access_token;
        FB.setAccessToken(accessToken);
        cb()
    });


};

results matching ""

    No results matching ""