UNPKG

scf-service-stack

Version:

A set for developing a cloud function

53 lines (48 loc) 1.53 kB
const SCFRouter = require('scf-service-stack').router; const { bodyParser, staticHandler, cookieParser, LocalStorageClient, TCBStorageClient, sessionParser } = require('scf-service-stack').middlewares; const http = require('./routers/http.router'); const test = require('./routers/test.router'); const path = require('path'); module.exports = async (request, context) => { const app = new SCFRouter(request, { templateFolder: path.join(__dirname, 'templates'), defaultTemplateData: { data: {} } }); console.log(request); // 以下四个中间件为可选项,可自行搭配 app.use(staticHandler(path.join(__dirname, 'public'))); app.use(bodyParser); app.use(cookieParser); app.use(sessionParser(new LocalStorageClient(), { maxAge: 600 })); /* if needs remote storage app.use( sessionParser( new TCBStorageClient( process.env['REMOTE_CACHE_SECRET_ID'], process.env['REMOTE_CACHE_SECRET_KEY'], process.env['REMOTE_CACHE_ENVID'], process.env['REMOTE_CACHE_COLLECTION'] ), {maxAge: 600} ) ); */ app.extends(http); app.extends(test); app.post(['/tink', '/tink/cai'], async (req, res, next) => { res.json(['1', '2']); next(); }); // 写在最后作为最后的handler,处理未定义的path,也建议在这里用get和post分别输出404 app.use(async (req, res, next) => { throw new Error('no mapping routers'); }); return app.serve(); };