-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (156 loc) · 5.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const client = require("cheerio-httpcli");
const http = require("http");
const fs = require("fs");
const url = require("url");
const ejs = require("ejs");
const qs = require("querystring");
const index_page = fs.readFileSync("./index.ejs", "utf8");
const line_bot_page = fs.readFileSync("./line_bot/index.ejs", "utf8");
const newdata_page = fs.readFileSync("./newdata/index.html", "utf8");
const normalize_css = fs.readFileSync('./public/stylesheet/normalize.css', "utf8");
const style_css = fs.readFileSync('./public/stylesheet/style.css', "utf8");
const newdata_style_css = fs.readFileSync('./newdata/style.css', "utf8");
const script_js = fs.readFileSync('./public/javascript/script.js', "utf8");
var server = http.createServer(getFormClient);
const port = process.env.PORT || 3000;
server.listen(port);
console.log("Server Start!");
///////////////////////////ここまでがメインプログラム
// Googleで「入力された地名」について検索する。
function getLatLng(request, response, msg) {
//自動入力用に配列用意
var word = msg.split(" ");
//エラー時の出力場所
var lat = "34.397667";
var lng = "132.475379";
if (word.length == 3) {
msg = word[1] + " " + word[2];
}
client.fetch("https://www.geocoding.jp/", { q: msg }, function (
err,
$,
res,
body
) {
//HTML緯度経度を表示ここで文字を抽出
lat = $("span[class=latlng]").text().split(" ", 2)[1];
lng = $("span[class=latlng] b:nth-child(2)").text();
write_index(request, response, lat, lng, word[0]);
});
}
function getFormClient(request, response) {
var url_parts = url.parse(request.url, true);
switch (url_parts.pathname) {
case "/":
response_index(request, response);
break;
case "/line_bot/index.ejs":
response.writeHead(200, { "Content-Type": "text/html" });
response.write(line_bot_page);
response.end();
break;
case "/newdata/index.html":
response.writeHead(200, { "Content-Type": "text/html" });
response.write(newdata_page);
response.end();
break;
case "/normalize.css":
response.writeHead(200, { "Content-Type": "text/css" });
response.write(normalize_css);
response.end();
break;
case "/style.css":
response.writeHead(200, { "Content-Type": "text/css" });
response.write(style_css);
response.end();
break;
case "/newdata/style.css":
response.writeHead(200, { "Content-Type": "text/css" });
response.write(newdata_style_css);
response.end();
break;
case "/script.js":
response.writeHead(200, { "Content-Type": "text/javascript" });
response.write(script_js);
response.end();
break;
// --------------------画像のパス --------------------
case "/images/back.png":
response.writeHead(200, { "Content-type": "text/png" });
response.end(fs.readFileSync("./public/images/back.png"));
break;
case "/images/back_mob.png":
response.writeHead(200, { "Content-type": "text/png" });
response.end(fs.readFileSync("./public/images/back_mob.png"));
break;
case "/images/smoking.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/smoking.png"));
case "/images/pin.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/pin.png"));
case "/images/moba-munu.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/moba-menu.png"));
case "/images/bot1.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/bot1.PNG"));
case "/images/bot2.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/bot2.PNG"));
case "/images/bot3.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/bot3.PNG"));
case "/images/bot4.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/bot4.PNG"));
case "/images/bot5.png":
response.writeHead(200, { "Content-Type": "text/png" });
response.end(fs.readFileSync("./public/images/bot5.PNG"));
default:
response.writeHead(200, { "Content-Type": "text/plian" });
response.end("no page...");
break;
}
}
function response_index(request, response) {
//初期値広島
var data = { msg: "広島駅" };
var body = "";
if (request.method == "POST") {
request.on("data", data => {
body += data;
});
request.on("end", () => {
data = qs.parse(body);
getLatLng(request, response, data.msg);
});
} else {
write_index(request, response, "34.397667", "132.475379", data.msg);
}
}
function write_index(request, response, lat, lng, word) {
var content4 = "の位置情報";
//エラー処理
if (lat == undefined) {
word = "*正しい地名を入力してください*";
content4 = "";
}
var content = ejs.render(index_page
, {
content: word,
content1: lat,
content2: lng,
content3:
"https://www.google.com/maps/d/embed?mid=1nfJ4qICIxHyiVMtdFl2Un8iSBXtCVT2I&ll=" +
lat +
"," +
lng +
"&z=18",
content4: content4
}
);
response.writeHead(200, { "Content-Type": "text/html" });
response.write(content);
response.end();
}