-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (41 loc) · 1.28 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
global.Promise = require("bluebird");
const http = require("http");
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const app = express();
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/htn", { useMongoClient: true })
.then(() => console.log("Mongodb connection established"));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "static")));
let Item = require("./db/Item");
app.get("/", (req, res) => {
Item.find({}, (err, items) => {
if(err) throw err;
return res.render("index", { items });
});
});
app.get("/items", (req, res) => {
Item.find({}, (err, items) => {
if(err) throw err;
return res.send(JSON.stringify(items));
});
});
app.get("/callback", (req, res) => {
let callback_data = JSON.parse(req.query["data"]);
return res.redirect("/");
switch(callback_data["status"]) {
case "error": {
return res.send(`Error in transaction ${callback_data["error_code"]}`)
}
case "ok": {
return res.send("Success!");
}
default: {
return res.send("default");
}
}
});
http.createServer(app).listen(5000, () => console.log("Server listening in on port 5000"));