简体   繁体   English

在Node.js中,如何在 <meta http-equiv> 标签

[英]in Node.js, how to get set-cookie value in a <meta http-equiv> tag

In Node.js, I need to catch instances where a cookie is set in a response, so I can manipulate it. 在Node.js中,我需要捕获在响应中设置cookie的实例,以便可以对其进行操作。 I am addressing these two ways in which cookies are set: 我正在解决设置cookie的两种方法:

  1. via the set-cookie HTTP header 通过set-cookie HTTP标头
  2. via the <meta http-equiv="Set-Cookie"> 通过<meta http-equiv="Set-Cookie">

I am able to get the first one working using: 我可以使用以下方法使第一个工作:

if (getProperty(responseHeaders, 'set-cookie') != null) {
    //do something
}

How would I address the second method? 我将如何解决第二种方法? Do I need to scrape the HTML response, or is there a better way? 我需要抓取HTML响应,还是有更好的方法? If the former, how? 如果是前者,怎么办?

I solved this by using regex on the response body: 我通过在响应主体上使用正则表达式解决了这个问题:

var regexHandler = function(match, cookieValue){
    return ""; //remove the meta tag
};

responseBody = responseBody.replace(/<meta.*http-equiv=[\"']?set-cookie[\"']?.*content=[\"'](.*)[\"'].*>/gi, regexHandler);
responseBody = responseBody.replace(/<meta.*content=[\"'](.*)[\"'].*http-equiv=[\"']?set-cookie[\"']? .*>/gi, regexHandler);

Cookies are transported as part of the HTTP headers. Cookies作为HTTP标头的一部分进行传输。 It is a HTTP thing, not an HTML thing. 这是HTTP内容,而不是HTML内容。

When cookies come from the client to your server, the header field is called 'Cookie'. 当Cookie从客户端发送到您的服务器时,标头字段称为“ Cookie”。 When you respons to the incomming request, you must always include the 'Set-Cookie' tag in your response, if you want to set or keep the cookie in the clients browser. 如果要在客户端浏览器中设置或保留cookie,则在响应传入请求时,必须始终在响应中包括“ Set-Cookie”标签。

If you don't set the cookie, it will be removed in the clients browser. 如果您未设置cookie,它将在客户端浏览器中删除。

You can look for cookies in req.header['set-cookie'] , and you can set cookies the same way using the response. 您可以在req.header['set-cookie']req.header['set-cookie'] ,也可以使用响应以相同的方式设置cookie。

There are a lot of good libraries out there which will do all of this for you. 有很多好的库可以为您完成所有这些工作。 Take a look at https://github.com/jed/cookies , which works for Express, Connect and normal NodeJS :) 看一下https://github.com/jed/cookies ,它适用于Express,Connect和普通的NodeJS :)

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

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