Node.js 學習第二十九天
到這最後幾天,看了一下課程表就知道一定會超過 30 天了,管它的繼續寫我打算 Node.js 了解後去找看看爬蟲的文章來學習。
如果在看的過程中有任何錯誤歡迎寫 E-MAIL: pp840405@gmail.com 提醒我錯誤。
Cookie 講解
以下圖片由六角學院擁有
用 Google Chrome 去測試圖片中的範例程式碼
Cookie Node.js Server 設計
接收前端的 Cookie 資料
首先使用 express-generator 套件建立一個 express 環境
1
2
3
4安裝 express 有 EJS 環境的資料夾
express -e mycookie
進入並安裝 package 內建的套件參數
cd mycookie && npm install到 index.js 去新增資料
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
//撈到網頁的cookie資料
console.log(req.cookies);
//傳送cookie到前端資料
res.cookie('name','tom',{
//暫存時間千分之一秒
maxAge:1000,
//防止其他人取得cookie資料
httpOnly:true
})
res.render('index', { title: 'Express' });
});
module.exports = router;
結語
就….超過 30 天繼續學囉,有任何問題歡迎寫 MAIL: pp840405@gmail.com 一起共同討論。