Esempio n. 1
0
// Create koa app
const app = koa();

// middleware
app.use(logger());

//设置静态目录内容
app.use(serve('./pages')).use(serve('./dev')).use(serve('./mock'));

app.keys = ['site.com'];
app.use(session({
	store: {
		host: process.env.SESSION_PORT_6379_TCP_ADDR || '127.0.0.1',
		port: process.env.SESSION_PORT_6379_TCP_PORT || 6380,
		ttl: 3600,
		keySchema: 'XD:schema',
		key: 'XD:session'
	},
	cookie: {
		path: '/'
	}
}));

app.use(koaBody({
	formidable: {
		uploadDir: __dirname
	},
	jsonLimit: '10mb',
	formLimit: '10mb'
}));
/**
 * 运行时错误处理,这里很重要
Esempio n. 2
0
const routes = require('./routes/index');
// redis初始化
require('./lib/redis.js').init(config.redis);
// 初始化数据库
require('./models/index');
routes(router);

app.keys = [config.apiService.sessionKey];

// 中间件
app
    .use(logger.middleLogger())
    .use(convert(gzip()))
    .use(serve(`${__dirname}/../public/dist`))
    .use(views(`${__dirname}/../public/views`, {map: {html: 'ejs'}}))
    .use(convert(session({store: config.redis})))
    .use(convert(koaBody({multipart: true,formidable: {keepExtensions: true}})))
    .use(middleObj.timeCheck())
    .use(router.routes(), router.allowedMethods());

// 创建服务
const server = require('http').createServer(app.callback());

// 启动消息中心
require('./lib/socket.js')(server);

// 启动监听端口
server.listen(config.apiService.port, function(){
    console.log(`Server has started, listen for ${config.apiService.port}`);
});