3

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

});
4
  • Where's your code there's something wrong with your strings. Commented May 18, 2018 at 19:49
  • the code is reading the file in the below format : //URL// account/42 //Status// 200 //BODY// { "name": "XYZ" } //HEADERS// Commented May 18, 2018 at 20:11
  • And your file looks like what? Commented May 18, 2018 at 20:13
  • Simple Text file which has all the information. Commented May 18, 2018 at 20:38

3 Answers 3

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Brian, I understood what mistake I was making and It's working now :-)
1

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"
]

Comments

-1

Template literals use a back-tick not quotes.

2 Comments

It is in backticks.
The value of headers are in backticks. headers -> 'Content-Type', 'application/json'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.