Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw HTTP header string in Node.js with express.js

I currently work with Node.js and express.js. For my current project I need to access the raw strings of the HTTP headers (charset and accepted).

There is a function in express.js which returns those charsets and accepted headers, however, these are sorted by quality and therefore not useable for me in this special case I need.

req.accepted // Returns sorted array of accepted header

req.acceptedCharsets // Returns sorted array of accepted lang header

However, I need the raw strings (iso-8859-5;q=.2, unicode-1-1;q=0.8, text/*;q=.5, application/json).

Now is there a way how I can access those raw strings in my express app?

like image 269
cschaeffler Avatar asked May 13 '13 08:05

cschaeffler


People also ask

What is Express () in node JS?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

What is HTTP header in node JS?

The header tells the server details about the request such as what type of data the client, user, or request wants in the response. Type can be html , text , JSON , cookies or others.

What is RES setHeader in node JS?

setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.


1 Answers

req.headers

as in

    var express = require('express');

var app = express.createServer();

app.get('/', function(req, res){
    console.log(req.headers);
    res.header('time', 12345);

    res.send('Hello World');
});

app.listen(3000);
like image 59
Lassi Kinnunen Avatar answered Oct 15 '22 03:10

Lassi Kinnunen