简体   繁体   English

如何使用 express.js 在 Node.js 中获取原始 HTTP 标头字符串

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

I currently work with Node.js and express.js.我目前使用 Node.js 和 express.js。 For my current project I need to access the raw strings of the HTTP headers (charset and accepted).对于我当前的项目,我需要访问 HTTP 标头(字符集和接受)的原始字符串。

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. express.js 中有一个函数返回这些字符集和接受的标头,但是,这些是按质量排序的,因此在我需要的这种特殊情况下对我来说不可用。

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 ).但是,我需要原始字符串( 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?现在有没有办法在我的 Express 应用程序中访问这些原始字符串?

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);
const express = require('express')

const app = express()

app.listen(3000)

app.use((req, res, next) => {
  const headersJson = JSON.stringify(req.headers)
  console.log(headersJson)

  // Save into file using fs module

  // save into database

  next()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM