blob: 0d61cd3ce9f15961e25cbf790404964d20872ea9 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const http = require("http");
const fs = require("fs");
const server = http.createServer();
server.on("request", function(req, res) {
var arrPara = req.url.split("?")
console.log("arrPara : " + arrPara);
if (req.url == '/') {
res.setHeader('Content-Type', 'text/html; charset=utf-8')
fs.readFile("static/index.html", "utf-8", function(error, data) {
// 用error来判断文件是否读取成功
if (error) return console.log("err index.html : " + error.message);
console.log("index.html : " + data);
res.end(data)
});
} else if (arrPara[0] == '/calculator/bmi') {
var arrPara = req.url.split("?")
console.log("arrPara : " + arrPara);
if (arrPara.length != 2) {
res.end("para err : " + req.url);
}
var arrInfo = arrPara[1].split("&");
if (arrInfo.length != 2) {
res.end("para err : " + req.url)
}
var request = require("request");
request('http://calculator/bmi?' + arrPara[1], function(error, response, body) {
console.log(body);
if (!error && response.statusCode == 200) {
res.setHeader("Content-Type", "application/json")
res.setHeader("Access-Control-Allow-Origin", "*")
res.end(body);
};
if (error || response.statusCode != 200) {
res.end(body);
};
});
} else {
res.end("err url : " + req.url)
}
});
server.listen(4597);