Fatma Zaman
Fatma Zaman

Reputation: 69

Node.js HTTP - TypeError: Header name must be a valid HTTP Token

when I'm trying to set the header in the following way ,it's working absolutely fine.

response.setHeader('Content-Type', 'application/json');

but when I'm trying to add variable instead of exact header name/value in the following way, it's showing error :-(

response.setHeader(result.headers);

if you console.log("headers -> " + result.header) the result would be the same.

headers -> 'Content-Type', 'application/json'

following are the exact error I'm getting , not able to figure out how to get around it.

 _http_outgoing.js:487

throw new TypeError(`Header name must be a valid HTTP Token ["${name}"]`);

^



 TypeError: Header name must be a valid HTTP Token ["'Content-Type', 'application/json'"]

at validateHeader (_http_outgoing.js:487:11)

at ServerResponse.setHeader (_http_outgoing.js:498:3)

at C:\service-mocker\src\main\app.js:54:22

at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

Below is whole code I'm trying to implement :

var http = require('http');

var fs = require('fs');

var path = require('path');

var repl = require('repl');

var map={};

var key;

var value;



//create a server object:

var server = http.createServer(function(request, response) {



    fs.readFile("./resources/FileData1.txt", function(err, data) {

        if(err) throw err;

        content = data.toString().split(/(?:\r\n|\r|\n)/g).map(function(line){

            return line.trim();

        }).filter(Boolean)

        var result = processFile(content);

        console.log("url -> " + result.url);

       console.log("status -> " + result.status);

       console.log("headers -> " + result.headers);

       console.log("body -> " + result.body);



       function objToString (obj) {

        var str = '';

        for (var p in obj) {

            if (obj.hasOwnProperty(p)) {

                str +=  obj[p] + '\n';

            }

        }

        return str;

    }

    function processFile(nodes) {



        nodes.forEach(function(node) {

            if(node.startsWith("//")){

                key = node.substring(2, node.length-2).toLowerCase().trim();

                return;    

            }

            else{

                value = node;

            }

            // map[key] = value;

            if(key in map){

                map[key].push(value);

            }else{

                map[key]= [value]; 

            }

        });



        return map;

        // console.log(map);

    }

   if(request.url == result.url ){

        response.setHeader(result.headers);

        // response.setHeader('Content-Type', 'application/json');

        response.write( objToString(result.body) );

        response.statuscode = parseInt( result.status );

        response.end();

    }else {

        // response.writeHead(404, {"Content-Type": "text/html"});

        response.end("No Page Found");

        }

      });

 });


var port = process.env.PORT || 8080;

server.listen(port, function() {

    console.log('Listening on port ' + port);

});

Upvotes: 3

Views: 18474

Answers (3)

Jemi Salo
Jemi Salo

Reputation: 3751

If you dont want to set headers one at a time with response.setHeader, you should use response.writeHead, which will write the status code and your headers into the response.

response.writeHead(200, headers)

Here headers must be a JavaScript object.

Replace

response.setHeader(result.headers);
response.statuscode = parseInt( result.status );

with

response.writeHead(parseInt(result.status), parseHeaders(result.headers));

where parseHeaders is a function that parses result.headers into an object.

function parseHeaders(headers) {
    var o = {};
    headers.forEach(header => {o[header.split(',')[0]] = header.split(',')[1]});
    return o;
}

In this answer I've assumed result.headers is formatted like so

[
    "header1, header1value",
    "header2, header2value"
]

Upvotes: 1

Brian
Brian

Reputation: 3914

It looks like result.headers is returning a single string, but response.setHeader needs two arguments: the header name and the header value.

Based on the error message, you're doing the equivalent of:

response.setHeader("'Content-Type', 'application/json'");

Instead, you need to split out the header into key and value:

//n.b. make sure that your result only has the one header!
var headerSplit = result.headers.split(',');
var headerKey = headerSplit[0];
var headerVal = headerSplit[1];

response.setHeader(headerKey, headerVal);

Upvotes: 4

Darkrum
Darkrum

Reputation: 1373

Template literals use a back-tick not quotes.

Upvotes: -1

Related Questions