簡體   English   中英

NodeJS設置/添加cookie

[英]NodeJS set / add cookies

我需要簡單的Node JS服務器,該服務器返回一些html頁面並為其設置一個或多個cookie。

如何在Node JS中添加一個或多個Cookie?

例如,您在其中有目錄和2個文件:

my_folder:

  • main.js
  • index.html

腳步:

  1. 安裝cookie: npm安裝cookie
  2. main.js應該是下一個:

 var http = require('http'), fs = require('fs'), cookie = require('cookie'); http.createServer(function (request, response) { var setCookies = []; setCookies.push( cookie.serialize('name1', 'Jhon1') ); setCookies.push( cookie.serialize('name2', 'Jhon2') ); response.setHeader('Set-Cookie', setCookies); response.writeHead(200, { 'Content-Type': 'text/html' }); fs.createReadStream('./index.html').pipe(response); }).listen(8000); console.log('Server is started: "http://localhost:8000"'); 

  1. 運行:$ node ./main.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM