Issue
I am trying to get header of get request. But i receive the error:
TypeError: request.getHeader is not a function
at Server.<anonymous> (/home/pi/progs/smarthome/Site/cpu.js:8:27)
at Server.emit (events.js:198:13)
at parserOnIncoming (_http_server.js:695:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:17)
here is my code:
var http = require("http");
var fs = require("fs");
var server = http.createServer(function(request, response) {
var temp = fs.readFileSync("/sys/class/thermal/thermal_zone0/temp");
var temp_c = temp/1000;
var header = request.getHeader('Content-Type');
console.log(header);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Raspberry Pi cpu temperature: ");
response.write("\n" + temp);
response.write("\n" + temp_c);
response.end();
});
server.listen(8080);
What am I doing wrong?
Solution
Your req.getHeader() is not working because there is no method inside the incoming http message object. You are getting an http.IncomingMessage object which does not have a req.getHeader(). Doc
Answered By - Ashish Rawat Answer Checked By - Mary Flores (WPSolving Volunteer)