应用配置 - nokitjs/nokit GitHub Wiki

简述

每一个 Nokit 应用,通常都需要一个 “应用程序配置文件”,可以是一个 .json 文件,也可以是一个 .js 文件,名为 config.json 或者 config.js ,使用命令行工具 nokit create 创建一个 "应用" 时,生成的项目模板会包含 config.json。

环境配置

除了默认的 config.json 配置之外,还可以指一定个 “针对特定环境的配置”,名称为 config.xxx.json,xxx 可以通过 "NODE_ENV" 环境变量指定,如果使用了 命令行工具 也可以通过 -env=xxx 选项指定。 config.xxx.json 中不必添加全部配置,config.json 作为默认配置,在 config.xxx.json 中 “覆盖” 需要的 “配置节” 即可。

app.json 示例,

{
    //允许的 host ,如 abc.com
    "hosts": [],
    //绑定到的端口,如查用 ```nokit start [端口]``` 指定了端口,将会忽略此处的配置
    "port": 8000,
    //默认文档
    "defaults": {
        "html": "index.html",
        "nsp": "index.nsp"
    },
    //HTTP 请求头信息,这是一个全局设置,在此设置的头信息,针对每一个请求都会有效
    "headers": {},
    //设定处理器,一般不会配置此节,当需要自定义 handler 时,才需配置
    "handlers": {
        "*": "./handlers/static"
    },

    //此节不除非特殊需要,不要动
    "parsers": {
        "*": "./parsers/urlencoded"
    },

    //指定全司应用程序类,一般不必更改配置
    "global": "./global.js",

    //过滤器配置,这时一个常用的配置,比如在需要截获请求时
    "filters": {
        "^/": "./xxxx.js"
    },

    //在请配置节可以自定义 “错误页面” 
    "template": {
        "pages": {
            "500": "./templates/500.html",
            "404": "./templates/404.html",
            "403": "./templates/403.html",
            "405": "./templates/405.html",
            "explore": "./templates/explore.html"
        },
        "compress": true
    },

    //指定 public 目录(用于存放静态资源)
    "folders": {
        "public": {
            "^/":"./public"
        }
    },

    //缓存配置
    "cache": {
        //需要进行缓存的文件(正则表达式)
        "match": {
            ".html$": true,
            ".htm$": true,
            ".css$": true,
            ".js$": true,
            ".json$": true,
            ".xml$": true,
            ".svg$": true,
            ".txt$": true,
            ".text$": true,
            ".csv$": true,
            ".icon$": true,
            ".ico$": true,
            ".less$": true,
            ".sass$": true,
            ".md$": true,
            ".png$": true,
            ".jpg$": true,
            ".jpeg$": true,
            ".gif$": true,
            ".bmp$": true,
            ".swf$": true,
            ".ttf$": true,
            ".woff$": true,
            ".eot": true
        },

        //缓存时间(0 则不进行缓存,不影响 lastModified)
        "maxAge": 1800,

        //条件缓存,可选值为 true|false ,启用后 match 匹配的文件会进行缓存,当文件发生变动时更新缓存
        "lastModified": true
    },

    //拒绝访问的类型,不要动这个配置节
    "forbiddens": {
        ".nsp.js$": true,
        ".nsp.html$": true,
        ".nsp.htm$": true,
        ".nsh.js$": true,
        ".log$": true
    },

    //配置 session 提供程序,当需要自定义 session 存储时需更改配置
    "session": {
        "enabled": true,
        "timeout": 1800,
        "provider": "$/core/session"
    },

    //提定启用 gzip 压缩的请求
    "compress": {
        "enabled": true,
        "type": {
            "text/html": true,
            "text/css": true,
            "text/javascript": true,
            "appliction/x-javascript": true,
            "text/markdown": true,
            "text/less": true,
            "text/sass": true,
            "text/plain": true,
            "application/json": true,
            "application/xml": true,
            "image/svg+xml": true,
            "text/comma-separated-values": true
        }
    },

    //目录浏览配置
    "browseFolder": {
        "^/": true
    },

    //配置 log 提供程序,当需要自定义 log 存储时需更改配置
    "log": {
        "enabled": true,
        "provider": "$./providers/log-writer-file",
        "path": "./logs"
    },

    //是否启动 https,及 https 配置
    "https": {
        "enabled": false,
        "redirectPort":80, // http -> https 的跳转端口,0 为不启用
        "cert": "",
        "key": "",
        "pfx": ""
    },

    //配置 mime 
    "mimeType": {
        ...
    }
}

注意,app.json 中,所有需要配置路径,都可用 $ 符开头指定这个路径相对于 nokit 的安装位置查找,如 “$./filters/http-to-https.js”,一般用于指定一个内置的组件。