博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs读取配置文件
阅读量:7136 次
发布时间:2019-06-28

本文共 3378 字,大约阅读时间需要 11 分钟。

INI.js(模块)

var eol = process.platform === "win32" ? "\r\n" : "\n" function INI() {    this.sections = {};} /** * 删除Section * @param sectionName */INI.prototype.removeSection = function (sectionName) {     sectionName =  sectionName.replace(/\[/g,'(');    sectionName = sectionName.replace(/]/g,')');     if (this.sections[sectionName]) {        delete this.sections[sectionName];    }}/** * 创建或者得到某个Section * @type {Function} */INI.prototype.getOrCreateSection = INI.prototype.section = function (sectionName) {     sectionName =  sectionName.replace(/\[/g,'(');    sectionName = sectionName.replace(/]/g,')');     if (!this.sections[sectionName]) {        this.sections[sectionName] = {};    }    return this.sections[sectionName]} /** * 将INI转换成文本 * * @returns {string} */INI.prototype.encodeToIni = INI.prototype.toString = function encodeIni() {    var _INI = this;    var sectionOut = _INI.encodeSection(null, _INI);    Object.keys(_INI.sections).forEach(function (k, _, __) {        if (_INI.sections) {            sectionOut += _INI.encodeSection(k, _INI.sections[k])        }    });    return sectionOut;} /** * * @param section * @param obj * @returns {string} */INI.prototype.encodeSection = function (section, obj) {    var out = "";    Object.keys(obj).forEach(function (k, _, __) {        var val = obj[k]        if (val && Array.isArray(val)) {            val.forEach(function (item) {                out += safe(k + "[]") + " = " + safe(item) + "\n"            })        } else if (val && typeof val === "object") {        } else {            out += safe(k) + " = " + safe(val) + eol        }    })    if (section && out.length) {        out = "[" + safe(section) + "]" + eol + out    }    return out+"\n";}function safe(val) {    return (typeof val !== "string" || val.match(/[\r\n]/) || val.match(/^\[/) || (val.length > 1 && val.charAt(0) === "\"" && val.slice(-1) === "\"") || val !== val.trim()) ? JSON.stringify(val) : val.replace(/;/g, '\\;')} var regex = {    section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,    param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/,    comment: /^\s*;.*$/}; /** * * @param data * @returns {INI} */exports.parse = function (data) {    var value = new INI();    var lines = data.split(/\r\n|\r|\n/);    var section = null;    lines.forEach(function (line) {        if (regex.comment.test(line)) {            return;        } else if (regex.param.test(line)) {            var match = line.match(regex.param);            if (section) {                section[match[1]] = match[2];            } else {                value[match[1]] = match[2];            }        } else if (regex.section.test(line)) {            var match = line.match(regex.section);            section = value.getOrCreateSection(match[1])        } else if (line.length == 0 && section) {            section = null;        }        ;    });    return value;} /** * 创建INI * @type {Function} */exports.createINI = exports.create = function () {    return new INI();}; var fs = require('fs'); exports.loadFileSync =function(fileName/*,charset*/){    return exports.parse(fs.readFileSync(fileName, "utf-8")) ;}

  

conf.ini:

[user]username=testpassword=123456

 实际调用实例:

var INI = require("../ini/INI");//INI模块var ini___ = INI.loadFileSync("conf.ini")//从conf.ini读取配置var se = ini___.getOrCreateSection("user");//取得httpservervar username = se['username'];var password= se['password'];

  

 

转载地址:http://obvrl.baihongyu.com/

你可能感兴趣的文章
array_filter
查看>>
[CC-ANUCBC]Cards, bags and coins
查看>>
Riemann-Stieltjes积分存在的充分条件(按照Tom M.Apostol的《数学分析》上的定义)
查看>>
[转]前端开发十日谈 下
查看>>
ahjesus —— javascript命名规范1.10
查看>>
caller 和 callee的对比
查看>>
使用GDB调试gp(转载)
查看>>
用Python给你的博客加上水印
查看>>
线性微分方程与常数变异法
查看>>
选夫婿1 结构体
查看>>
最少硬币数目的问题
查看>>
算法之折半查找
查看>>
webpack实用小功能介绍
查看>>
OpenStack high-level Functionsenabled
查看>>
深入理解Linux内核-内核同步
查看>>
zabbix实现mysql数据库的监控(三)
查看>>
外观模式-多了个办事处
查看>>
laravel 文件上传
查看>>
《寻路算法第二篇》A*寻路的路径平滑、静态合并、生成格子工具自动化、
查看>>
求职防骗指南
查看>>