nodejs+express-> http 访问本地json数据

首先要把json 放入public文件目录下:

在config.js里面定义domainUrl

exports.domainUrl = 'http://localhost:3000';

在 index.js中

var config = require('../config');
var domainUrl = config.domainUrl;
router.get('/index', function(req, res) {
    if(req.session.username){
        res.locals.username = req.session.username;
        var query = http.get(domainUrl+'/data/node-data.json',function (response){
            response.on('data',function (result){
                var data = JSON.parse(result.toString());
                var pageData = {};
                pageData['title'] = "前端技术学习";
                pageData['listData'] = data;
                res.render('../index',pageData);
            });
        });
        query.end();
    }else {
        res.redirect('/login');
        return;
    }
});

 

posted @ 2014-12-02 17:53  小菜学IT  阅读(1269)  评论(0编辑  收藏  举报