下面一段代码是什么语言?用什么软件运行?
const http = require('http');
const url = require('url');
const fs = require('fs');
let platGame = function (playAction) {
var radom = Math.random() * 3;
var obj = {};
obj.playAction = playAction;
var action, result;
if (radom < 1) {
action = "剪刀";
} else if (radom > 2) {
action = "石头";
} else {
action = "布";
}
obj.action = action;
if (playAction == action) {
result = "平局";
} else if (action == "剪刀" && playAction == "石头" || action == "石头" && playAction == "布" || action == "布" && playAction == "剪刀") {
result = "你赢了"
} else {
result = "你输了"
}
obj.result = result;
return JSON.stringify(obj);
}
var parseQuery = function (query) {
var reg = /([^=&\s]+)[=\s]*([^&\s]*)/g;
var obj = {};
while (reg.exec(query)) {
obj[RegExp.$1] = decodeURI(RegExp.$2);
}
return obj;
}
http.createServer(function (req, res) {
let parse = url.parse(req.url);
let pathname = parse.pathname
console.log(pathname);
if (pathname === '/favicon.ico') {
res.writeHead(200);
res.end();
};
if (pathname == "/action") {
var val = parseQuery(parse.query).playAction;
res.writeHead(200, { "content-type": "text/html;charset=utf-8" });
res.end(platGame(val));
}
if (pathname === '/') {
fs.createReadStream(__dirname + "/index.html").pipe(res)
}
}).listen(3000)