我是nodejs的初学者。我正在构建一个将json数据写入csv文件的简单服务器。问题是:
对于授权,需要在请求标头中设置“appKey”参数: appKey:9a3ab6d8-9ffe-49a5-8194-bc7d61123f4a
我无法理解我将要做什么。
这是我到目前为止所做的:
var fs = require('fs');
var express = require('express');
var app = express();
var inFilename = 'power_plants.json',
outFilename = 'powerplants.csv';
app.get('/', function (req, res) {
writeToCsv();
res.send('Successfully Created!');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
function writeToCsv(){
var inJSON = fs.readFileSync(inFilename);
inJSON = JSON.parse(inJSON);
var outCSV = inJSON.rows;
var csv = [];
for(var k in outCSV) {
var items = [[outCSV[k].PowerPlant , outCSV[k].meter]];
for (index = 0; index < items.length; ++index) {
csv.push(items[index].join(', ') + '\n');
}
}
fs.writeFile(outFilename, csv, function (err) {
if (err) {
return console.log(err);
}
console.log('FILE SUCCESSFULLY WRITTEN!\n');
});
}
答案 0 :(得分:2)
要提取标题appKey的值,你必须得到它:
var appKey = req.headers.appKey;