标签:style blog http io ar color os sp strong
首先要把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;
}
});
nodejs+express-> http 访问本地json数据
标签:style blog http io ar color os sp strong
原文地址:http://www.cnblogs.com/litttlespring/p/4138116.html