NodeJS测试实例

发表于:2017-1-13 10:53

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:jack_Meng    来源:51Testing软件测试网采编

  实例一:
  先来个简单的实例,把下面的代码保存为main.js,让自己欣喜下:
var http = require("http");
function onRequest(request, response){
console.log("Request received.");
var str='{"id":"0036",name:"jack",arg:123}';
response.writeHead(200,{"Content-Type":'text/plain','charset':'utf-8','Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});
//response.writeHead(200,{"Content-Type":'application/json','Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});
//response.write("Hello World 8888\n");
response.write(str);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.port on 8888");
  运行方式是在命令行中,直接输入:node main.js,然后打开IE浏览器输入http://127.0.0.1:8888,就可以到熟悉的内容了。
  ======================================================================
  实例二:
  通过读去json文件,发送json数据到浏览器,把下面的代码保存为json.js,
var http = require("http");
var fs = require("fs");
var str='{"id":"123",name:"jack",arg:11111}';
function onRequest(request, response){
console.log("Request received.");
response.writeHead(200,{"Content-Type":'text/plain','charset':'utf-8','Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});//可以解决跨域的请求
//response.writeHead(200,{"Content-Type":'application/json',            'Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});
//response.write("Hello World 8888\n");
str=fs.readFileSync('data.txt');
response.write(str);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.port on 8888\n");
console.log("test data: "+str.toString());
  然后再相同目录下保存一个data.txt文件,内容为:
  {"id":"0036",name:"jack",arg:32100,
  remark:"test data"}
  运行方式是在命令行中,直接输入:node json.js,然后打开IE浏览器输入http://127.0.0.1:8888,就可以到熟悉的内容了。
  ======================================================================
  实例三:
  读写ini文件,首先使用ini文件库,代码如下,保存为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(k=="___comment")return;
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
}
if (section || obj.___comment){
out = obj.___comment + 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 regex1 = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([\w\.\-\_\@]+)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
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;
var comm = null;
lines.forEach(function (line) {
if (regex.comment.test(line)) {
var match = line.match(regex.comment);
comm = match[0];
return;
} else if (regex.param.test(line)) {
var match = line.match(regex.param);
if (section) {
section[match[1]] = match[2];
if(comm)section[match[1]].___comment=comm;
} else {
value[match[1]] = match[2];
if(comm)value.___comment =comm;
}
comm = null;
} else if (regex.section.test(line)) {
var match = line.match(regex.section);
section = value.getOrCreateSection(match[1]);
if(comm)section.___comment=comm;
comm = null;
} else if (line.length == 0 && section) {
section = null;
comm = 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")) ;
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号