Mert AKEL
Mert AKEL

Reputation: 165

How to set key on request header for authorization in nodejs?

I am beginner in nodejs. I am building a simple server that writes json data to csv file. The question is:

For authorization, the “appKey” parameter needs to be set in the request header: appKey: 9a3ab6d8-9ffe-49a5-8194-bc7d61123f4a

I could not understand what I am going to do.

This is what I have so far:

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');
    });


}

Upvotes: 1

Views: 1472

Answers (1)

David Vicente
David Vicente

Reputation: 3111

To extract the value of the header appKey you have to get it with this:

var appKey = req.headers.appKey;

Upvotes: 2

Related Questions