nodejs读取配置文件
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=test
password=123456
实际调用实例:
var INI = require("../ini/INI");//INI模块
var ini___ = INI.loadFileSync("conf.ini")//从conf.ini读取配置
var se = ini___.getOrCreateSection("user");//取得httpserver
var username = se['username'];
var password= se['password'];
nodejs读取配置文件的更多相关文章
- 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
- 解决IntelliJ IDEA无法读取配置文件的问题
解决IntelliJ IDEA无法读取配置文件的问题 最近在学Mybatis,按照视频的讲解在项目的某个包里建立配置文件,然后读取配置文件,但是一直提示异常. 读取配置文件的为官方代码: String ...
- java-工具类-读取配置文件
java读取配置文件,当发现文件被修改后则重新加载 package com.zg.config; import java.io.File; import java.io.FileInputStream ...
- java 4种方式读取配置文件 + 修改配置文件
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...
- 在IIS Express中调试时无法读取配置文件 错误
在IIS Express中调试代码时,如果出现"无法读取配置文件"的问题(如图),这种情况是IIS Express的"applicationhost.config&quo ...
- ASP.NET Core开发-读取配置文件Configuration
ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...
- Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件
最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...
- Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)
在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [config ...
- win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面
错误一: HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (ov ...
随机推荐
- xml的并发
网站整站的缓存方式都是依靠的DataSet的ReadXml和WriteXml的方式实现的,这种方式在访问量不是很大的网站中是一点问题都没有的(最大可承受的日IP估计在8000-15000左右),但是当 ...
- 《Java性能权威指南》笔记----JIT编译器
概览 编译型语言(C++,Fortran等):运行程序前,需要用编译器将代码静态编译成CPU可执行的汇编码.汇编码针对特定的CPU. 优点:只需编译一次,且有足够的程序信息来优化汇编码.执行速度快: ...
- HDU4305 Lightning
There are N robots standing on the ground (Don't know why. Don't know how). Suddenly the sky turns i ...
- [Codeforces Round #351 Div. 2] 673A Bear and Game
A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- div两侧的boder断开 消失 奇怪
原文发布时间为:2009-11-06 -- 来源于本人的百度文章 [由搬家工具导入] 解决方法: 设定外层DIV的宽度即可,如 width:99% ========================== ...
- 在VS2013中使用boost库遇到的问题及解决(转)
原文转自 https://my.oschina.net/SunLightJuly/blog/676891?p=1 最近的项目需要集成一个使用了boost库的开源库.原本应该是比较简单的工作,因为使用的 ...
- 树莓派个人实测 Q&A(最新修改使用Manjaro连接远程桌面) (二)
以下内容使用和http://www.eeboard.com/bbs/thread-5191-1-1.html所在的帖子一样的风格,不过原作者是window下的操作,本人的都是在manjaro linu ...
- 查看windows进程,并删除
1. 通过[任务管理器]可以查看windows进程. 有些进程不在[任务管理器]中. 2. 通过tasklist命令查看进程. 杀掉进程: epmd 进程,在停止.卸载后rabbitmq服务还在. 通 ...
- LeetCode OJ-- Interleaving String **@
https://oj.leetcode.com/problems/interleaving-string/ 刚开始用递归做,但是超时了 class Solution { public: bool fl ...
- HDU 6251 Inkopolis(2017 CCPC-Final,I题,环套树 + 结论)
题目链接 HDU 6251 题意 给出一个$N$个点$N$条边的无向图.然后给出$M$个操作,每个操作为$(x, y, z)$,表示把连接 $x$和$y$的边的颜色改成$z$. 求这张无向图中所有边的 ...