var http
= require("http");
var url
= require('url');
var server
= http
.createServer();
server
.on('request', function (req
, resp
) {
console
.log("获取客户端请求");
var parseObj
= url
.parse(req
.url
, true);
var pathname
= parseObj
.pathname
;
req
.query
= parseObj
.query
;
switch (pathname
) {
case "/storeRoomInfo"://http
://127.0.0.1:3000
if (req
.query
.roomInfo
) {
var reg
= new RegExp('"', "g");
req
.query
.roomInfo
= req
.query
.roomInfo
.replace(reg
, "");
products
.push(req
.query
.roomInfo
);
}
resp
.end(JSON.stringify({ result
: 1, message
: "success" }));
break;
case "/getRoomList"://http
://127.0.0.1:3000/html
resp
.setHeader("Content-type", "text/plain;charset=utf-8");
resp
.end(JSON.stringify({ result
: 1, message
: "success", data
: { roomList
: products
} }));
break;
default:
resp
.end("404 Not Found.")
break;
}
})
server
.listen(3000, function () {
console
.log("服务器启动成功,可以通过 http://127.0.0.1:3000 进行访问");
})
var products
= [];
node server.js启动
转载请注明原文地址:https://tech.qufami.com/read-7977.html