const router = require('koa-router')(); const request = require('co-request'); const jwt = require('jsonwebtoken'); const jwtKoa = require('koa-jwt') const util = require('util') const verify = util.promisify(jwt.verify); const configUtil = require('../util/configUtil') const serverConfig = configUtil.loadConfig(); router.all('/api/*', async (ctx, next) => { let mapi = `${serverConfig.servicePath}${ctx.originalUrl}`; let bodyString = JSON.stringify(ctx.request.body) var opt = { uri: mapi, method: ctx.method, headers: ctx.headers, body: bodyString }; let result = await request(opt); let setcookie = result.headers["set-cookie"]; // Koa2 提供了從上下文中直接讀取和寫入 cookie 的方法 ctx.cookies.get(name, [options]) 和 ctx.cookies.set(name, value, [options]) let v = ''; if (setcookie) { setcookie.forEach( function (cookiestr) { } ); } ctx.status = result.statusCode; ctx.body = result.body; });
##另外一個web
const Koa = require('koa') const app = new Koa() app.use( async ( ctx ) => { if ( ctx.url === '/index' ) { ctx.cookies.set( 'cid', 'hello world', { domain: 'localhost', // 寫cookie所在的域名 path: '/index', // 寫cookie所在的路徑 maxAge: 10 * 60 * 1000, // cookie有效時長 expires: new Date('2017-02-15'), // cookie失效時間 httpOnly: false, // 是否只用於http請求中獲取 overwrite: false // 是否容許重寫 } ) ctx.body = 'cookie is ok' } else { ctx.body = 'hello world' } }) app.listen(3000, () => { console.log('[demo] cookie is starting at port 3000') })