forked from jilianggqq/light_local_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·94 lines (84 loc) · 2.03 KB
/
Copy pathserver.js
File metadata and controls
executable file
·94 lines (84 loc) · 2.03 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
// test
var express = require("express");
var df = require("./date_format.js");
var app = express();
var request = require("request");
app.use(express.static(__dirname + "/static"));
app.set("views", __dirname + "/views");
app.engine("html", require("ejs").renderFile);
var bodyParser = require("body-parser");
// it is necesscory. Otherwise you can not get the request body.
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
app.set("view engine", "ejs");
/*
just for test
*/
app.get("/hello", function(req, res) {
// return ;
res.send("Hello Express");
});
/*
leading to list page.
*/
app.get("/", function(req, res) {
res.render("index.html");
});
/*
get product
*/
app.get("/product", function(req, res) {
// read from DDB
read_product_ddb();
});
app.put("/product/", function(req, res) {
console.log(req.body);
//write information of req.body into DDB
write_into_ddb();
});
/*
leading to the insert form page.
*/
app.get("/new", function(req, res) {
console.log("new get");
res.render("newItem.html");
});
/*
leading to the insert form page.
*/
let base_get_url = "https://kgng8eutc9.execute-api.us-west-2.amazonaws.com/orders/";
app.get("/detail/:id", function(req, res) {
console.log(req.params.id);
request.get(base_get_url + req.params.id, function(error, response, body) {
if (error) {
res.send(err);
} else {
console.log(body);
let data = JSON.parse(body);
let dateTime = new Date(parseInt(data.date));
data.date_format = dateTime.format(df.masks.isoDateTime2);
res.render("detail", data);
}
});
});
/*
add a new item.
*/
let insert_url = "https://kgng8eutc9.execute-api.us-west-2.amazonaws.com/orders";
app.post("/new", function(req, res) {
request.put({ url: insert_url, json: req.body }, function(err, response, data) {
if (err) {
res.send(err);
} else {
console.log(data);
// res.render("index.html");
res.redirect("/");
}
});
});
app.listen(8083);
console.log("Server is running on port 8083");