-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
159 lines (130 loc) · 4.09 KB
/
server.js
File metadata and controls
159 lines (130 loc) · 4.09 KB
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
const express = require("express");
const next = require("next");
const bodyParser = require("body-parser");
const path = require("path");
const fs = require("fs");
const http = require("http");
const https = require("https");
const serveIndex = require("serve-index");
const {
getEstate,
getMunicipality,
getMunicipalityByNatCode,
getProvince,
sendOrder,
} = require("./data/data.js");
require("dotenv").config();
const dev = process.env.NODE_ENV !== "production";
const port = !dev ? 80 : 3000;
const app = next({ dev });
const handle = app.getRequestHandler();
app
.prepare()
.then(() => {
const server = express();
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.use(bodyParser.raw());
server.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
server.use(
"/.well-known",
express.static(".well-known"),
serveIndex(".well-known", { icons: true })
);
server.get("/api/kunnat/:id", async (req, res) => {
const id = req.params.id;
const { status: munStatus, resData: munData } = await getMunicipality(id);
let data = { kunta: munData, maakunta: null };
if (munStatus === 200) {
const { status: proStatus, resData: proData } = await getProvince(
munData.MAAKUNTANRO
);
data.maakunta = proData;
}
res.status(munStatus).end(JSON.stringify(data));
return;
});
server.get("/api/maakunnat/:id", async (req, res) => {
const id = req.params.id;
const { status, resData } = await getProvince(id);
res.status(status).end(JSON.stringify(resData));
return;
});
server.get("/api/kiinteistot/:id", async (req, res) => {
const id = req.params.id;
const { status: esStatus, resData: esData } = await getEstate(id);
const data = { kiinteisto: esData, kunta: null };
if (esStatus === 200) {
const { resData: munData } = await getMunicipalityByNatCode(
esData.k_natcode
);
data.kunta = munData;
}
res.status(esStatus).end(JSON.stringify(data));
return;
});
server.post("/api/tilaus", async (req, res) => {
data = req.body;
const { status, resData } = await sendOrder(data);
res.status(status).end(JSON.stringify(resData));
return;
});
server.get("*", (req, res) => {
return handle(req, res);
});
if (!dev) {
const privateKey = fs.readFileSync(
path.join(process.env.SSL_PATH, "privkey.pem"),
"utf8"
);
const certificate = fs.readFileSync(
path.join(process.env.SSL_PATH, "cert.pem"),
"utf8"
);
const ca = fs.readFileSync(
path.join(process.env.SSL_PATH, "chain.pem"),
"utf8"
);
const credentials = {
key: privateKey,
cert: certificate,
ca: ca,
};
const httpsServer = https.createServer(credentials, server);
httpsServer.listen(443, (err) => {
if (err) throw err;
console.log("> Https ready on https://localhost:443");
});
}
if (!dev) {
http
.createServer((req, res) => {
// 301 redirect (reclassifies google listings)
res.writeHead(301, {
Location: "https://" + req.headers["host"] + req.url,
});
res.end();
})
.listen(port, (err) => {
if (err) throw err;
console.log("> Redirection ready on http://localhost:" + port);
});
} else {
const httpServer = http.createServer(server);
httpServer.listen(port, (err) => {
if (err) throw err;
console.log("> Ready on http://localhost:" + port);
});
}
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});