-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
97 lines (81 loc) · 2.71 KB
/
app.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
// init project
const express = require("express");
const bodyParser = require("body-parser");
const swig = require("swig");
const path = require("path");
const app = express();
const port = process.env.PORT || 3000;
// init sqlite db
const dbFile = "./data.sqlite";
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database(dbFile);
const periodic_table = require("./routes/periodic-table");
// set templating engine
app.set("views", path.join(__dirname, "views"));
app.engine("html", new swig.Swig().renderFile);
app.set("view engine", "html");
// ad-hoc settings
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.get("/atoms", function(request, response) {
db.all("SELECT * from Element", function(err, rows) {
var data = JSON.stringify(rows);
var status = "success";
var message = "ok";
if (err) {
console.log(err.message);
status = "error";
message = err.message;
}
response.send({ status: status, message: message, data: data });
});
});
app.get("/atoms/:atomic_number", function(request, response) {
var atomic_number = request.params.atomic_number;
sql = `SELECT * FROM Element WHERE atomic_number = ?`;
db.get(sql, [atomic_number], function(err, row) {
var data = JSON.stringify(row);
var status = "success";
var message = "ok";
if (!row) {
status = "error";
message = "No data exists!";
}
if (err) {
console.error(err.message);
status = "error";
message = err.message;
}
response.send({ status: status, message: message, data: data });
});
});
app.get("/topics", function(request, response) {
db.all("SELECT * from Topics", function(err, rows) {
response.send(JSON.stringify(rows));
});
});
app.get("/", function(request, response) {
response.render("index");
});
app.get("/organic-reactions/:type", function(request, response) {
let type = request.params.type;
response.render("organic_reactions", { type: type });
});
app.get("/hybridization/:type", function(request, response) {
var type = request.params.type;
response.render("hybridization", { type: type });
});
app.get("/chemical-reactions/:type", function(request, response) {
var type = request.params.type;
response.render("chemical_reactions", { type: type });
});
app.get("/solid-state/:type", function(request, response) {
var type = request.params.type;
response.render("solid_state", { type: type });
});
app.get("/geometry/:type", function(request, response) {
var type = request.params.type;
response.render("geometry", { type: type });
});
app.use("/periodic-table", periodic_table);
app.listen(port, () => console.log(`App listening on port ${port}!`));