HTTP various features application (ii)

Source: Internet
Author: User

A. Cookies

With Set-cookie settings, the next browser request will be brought up, key-value pairs, you can set more than one.

Cookie Properties

Max-age and expires setting expiration time

Secure is only sent when HTTPS is

HttpOnly cannot be accessed through Document.cookie

Server.js Code

ConstHTTP = require ('http')ConstFS = require ('FS') Http.createserver (function (request, response) {Console.log ('Request Come', Request.url)if(Request.url = = ='/') {    Consthtml = Fs.readfilesync ('test.html','UTF8') Response.writehead ( $, {      'Content-type':'text/html',      'Set-cookie': ['id=123; max-age=2','abc=456;']}) Response.End (HTML)}). Listen (8888) Console.log ('Server listening on 8888')

test.html Code

<!DOCTYPE HTML><HTMLLang= "en"><Head>  <MetaCharSet= "UTF-8">  <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">  <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">  <title>Document</title></Head><Body>  <Div>Content</Div></Body><Script>Console.log (Document.cookie)</Script></HTML>

Request Result:

Second, HTTP long connection


ConstHTTP = require ('http')ConstFS = require ('FS') Http.createserver (function (request, response) {Console.log ('Request Come', Request.url)Consthtml = Fs.readfilesync ('test.html','UTF8')  Constimg = Fs.readfilesync ('test.jpg')  if(Request.url = = ='/') {Response.writehead ( $, {      'Content-type':'text/html',    }) Response.End (HTML)}Else{Response.writehead ( $, {      'Content-type':'image/jpg',      'Connection':'keep-alive' //or Close}) Response.End (IMG)}). Listen (8888) Console.log ('Server listening on 8888')


<!DOCTYPE HTML><HTMLLang= "en"><Head>  <MetaCharSet= "UTF-8">  <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">  <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">  <title>Document</title></Head><Body>  <imgsrc= "/test1.jpg"alt="">  <imgsrc= "/test2.jpg"alt="">  <imgsrc= "/test3.jpg"alt="">  <imgsrc= "/test4.jpg"alt="">  <imgsrc= "/test5.jpg"alt="">  <imgsrc= "/test6.jpg"alt="">  <imgsrc= "/test7.jpg"alt="">  <imgsrc= "/test11.jpg"alt="">  <imgsrc= "/test12.jpg"alt="">  <imgsrc= "/test13.jpg"alt="">  <imgsrc= "/test14.jpg"alt="">  <imgsrc= "/test15.jpg"alt="">  <imgsrc= "/test16.jpg"alt="">  <imgsrc= "/test17.jpg"alt="">  <imgsrc= "/test111.jpg"alt="">  <imgsrc= "/test112.jpg"alt="">  <imgsrc= "/test113.jpg"alt="">  <imgsrc= "/test114.jpg"alt="">  <imgsrc= "/test115.jpg"alt="">  <imgsrc= "/test116.jpg"alt="">  <imgsrc= "/test117.jpg"alt=""></Body></HTML>


Request Run Result:

HTTP various features application (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.