https://github.com/candyframework/candyjs-session-cookie
https://github.com/candyframework/candyjs-session-cookie
Last synced: 22 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/candyframework/candyjs-session-cookie
- Owner: candyframework
- Created: 2022-05-12T11:54:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-01T01:40:44.000Z (about 4 years ago)
- Last Synced: 2025-03-02T00:03:55.470Z (over 1 year ago)
- Language: TypeScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# candyjs-session-cookie
基于 cookie 的 session
## 安装
```
$ npm install @candyjs/session-cookie
```
## 使用
入口文件配置
```
// index.js
const CandyJs = require('candyjs');
const App = require('candyjs/web/Application');
const Hook = require('candyjs/core/Hook');
const Session = require('@candyjs/session-cookie');
Hook.addHook(Session.start({
key: 'some secure strings',
// 毫秒
maxAge: 86400000
}));
new CandyJs(new App({
'id': 1,
'debug': true,
'appPath': __dirname + '/app'
})).listen(2333, function(){
console.log('listen on 2333');
});
```
使用
```
// IndexController.js
const Controller = require('candyjs/web/Controller');
class IndexController extends Controller {
run(req, res) {
let views = req.session.getAttribute('views');
if(undefined === views) {
views = 0;
} else {
views += 1;
}
req.session.setAttribute('views', views);
res.write('views: ' + views);
res.end();
}
}
```
## API
```
session#getAttribute(name: string): any;
session#setAttribute(name: string, value: any): void;
session#deleteAttribute(name: string): void;
session#clear(): void;
```