var path = require('path'); var express = require('express'); var App = express(); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); var multer = require('connect-multiparty'); var Glob = require("glob"); var Utils=require('./Utils'); var Router = express.Router(); module.exports = function (apiCofig) { // 记录下当前文档的路径 //global.apps=apiCofig.apps; var Register={}; if(apiCofig.EnvConst){ for(var name in apiCofig.EnvConst){ Register[name]=apiCofig.EnvConst[name]; if(typeof Register[name]=="object"){ for(var key in Register[name]){ if(typeof Register[name][key]==="string"){ Register[name][key]=Utils.template(Register[name][key],Register) } } } } } App.set("Register",Register); /*日志配置和Console*/ var Console = require("./Console"); Console(apiCofig.log); console.info("YOHO"); global.Register=App.get("Register"); /*接口层*/ var Interfacer = require("./Interfacer"); var InterRegisters = new Interfacer(apiCofig,App); apiCofig.MVC.Interfacer && Glob.sync(apiCofig.MVC.Interfacer).forEach(function (src) { InterRegisters.register(require(src)); }); /*控制器层*/ var Controller = require("./Controller"); var ContRegisters = new Controller(InterRegisters); apiCofig.MVC.Controller && Glob.sync(apiCofig.MVC.Controller).forEach(function (src) { require(src)(ContRegisters); }); /*过滤器*/ var Filter = require("./Filter"); var FilterRegisters = new Filter(InterRegisters); apiCofig.MVC.filters && Glob.sync(apiCofig.MVC.filters).forEach(function (src) { require(src)(FilterRegisters); }); /*View 设置*/ var Viewer=require("./Viewer"); var ViewerRegisters=new Viewer(App, apiCofig.MVC.views); var template = require('art-template'); template.config('base', ''); template.config('extname', '.html'); template.config('openTag', '<%'); template.config('closeTag', '%>'); App.engine('.html', template.__express); App.set('view engine', 'html'); /*内置中间件 对Http请求解析*/ App.use(bodyParser.json()); App.use(bodyParser.urlencoded({extended: true})); App.use(cookieParser()); App.use(multer()); App.use("/" + apiCofig.static.name, express.static(apiCofig.static.value)); /*前置中間件*/ apiCofig.useToEntry && apiCofig.useToEntry(App); App.use(function(req, res, next){ ViewerRegisters.overrideRes.call(res); next(); }); /*路由控制*/ var KLH = function (obj, method, guid) { return function (req, res) { obj[method].call(obj, guid, req, res); } }; ContRegisters.routers.forEach(function (router) { var ROU = Router.route([router.url]); var args = FilterRegisters.use(router.url, router.method). concat(KLH(ContRegisters, "emit", router.guid)); ROU[router.method].apply(ROU, args); }); App.use(apiCofig.baseUrl, Router); App.use(function (req, res, next) { var err = new Error('Not Found'); err.status = 404; // console.error(err.message); next(err); }); /*後置中间间*/ apiCofig.useToOuter && apiCofig.useToOuter(App, ContRegisters); App.use(function (req, res, next) { res.end(); }); var server = App.listen(apiCofig.port, function () { var host = server.address().address; var port = server.address().port; console.log("HOST:"+host+"PORT:"+port); }); };