-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
80 lines (73 loc) · 2.54 KB
/
demo.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
const fs = require("fs");
const http = require("http");
const events = require("events");
const express = require("express");
const mongoose = require("mongoose");
const urlencodedParser = express.urlencoded({extended: false});
const app = express();
/* Connect to the mongodb*/
mongoose.connect("mongodb://Jimmy:[email protected]:27017,cluster0-shard-00-01.mzswg.azure.mongodb.net:27017,cluster0-shard-00-02.mzswg.azure.mongodb.net:27017/YT-tutorial?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true}).catch(function(err){if(err)console.log(err)});
const db = mongoose.connection;
db.on("error", function(err) {
console.log(err);
})
db.on("open", function() {
console.log("Connect to database");
})
/* Define schema and model */
const Schema = mongoose.Schema; // store this function to an variable
const itemSchema = new Schema({place: String, job: String}); // constructor call
const itemModel = mongoose.model("identity", itemSchema);
/* Set view engine */
app.set("view engine", "ejs");
/* middleware of all requests */
app.use(express.static(__dirname+"/public"));
/* Handle requests- GET, POST, DELETE */
app.get("/", function(req, res){
itemModel.find({}, function(err, data) {
res.render("main.ejs",{data: data});
});
});
app.post("/", urlencodedParser, function(req, res){
itemModel(req.body).save(function(err) {
if(err) throw err;
console.log("data saved");
res.send("Already receive the data");
});
});
app.delete("/", urlencodedParser, function(req, res) {
itemModel.deleteOne(req.body, function(err){
if(err) throw err;
res.send("Already delete the data");
})
});
app.get("/test/:name", function(req, res){
console.log("app.get on /test");
res.send("Welcome to the test page, "+req.params.name);
});
/* Handling ajax requests */
const jsonParser = express.json();
app.post("/contact", jsonParser, function(req, res){
console.log(req.body);
res.send("Response had been sent back from app.post");
}).get("/contact", function(req, res){
res.sendFile(__dirname+"/contact.html");
})
app.listen(process.env.PORT, function(err) {
if(err) throw err;
console.log("Work properly");
})
/*let obj = {age: 21, name: ["Jim", "Bob", "Alice"]};
app.set("view engine", "ejs");
app.use(express.static(__dirname+"/public") );
app.get("/", function(req, res) {
res.render("main.ejs", obj);
})
app.listen(3000, function(err) {
if(err) {
console.log(err);
}
else {
console.log("Work properly");
}
})*/