Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (72 loc) · 2.13 KB
/
Copy pathapp.js
File metadata and controls
86 lines (72 loc) · 2.13 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
var express = require("express");
var app = express();
var mongoose = require("mongoose");
app.set("view engine", "ejs");
app.use(express.static('public'));
// Creates the salsa app database inside of mongo
mongoose.connect("mongodb://localhost/salsa_app");
// Adding a new salsa event to the Database
var salsaSchema = new mongoose.Schema({
name: String,
location: String,
description: String,
time: String,
day: String
});
// Compile into a model
var salsaEvent = mongoose.model("salsaEvent", salsaSchema);
// USE TO CREATE A SALSA EVENT IN THE DATABASE
// salsaevent.create({
// name: " ",
// location: " ",
// description: " ",
// time: " ",
// day: " "
// }, function (err, salsaevent){
// if (err){
// console.log(err);
// } else {
// console.log("NEWLY CREATED SALSA EVENT");
// console.log(salsaevent);
// }
// }
// );
app.get("/", function(req, res){
res.render("home");
});
app.get("/salsaevents", function(req, res){
// Get all campgrounds from the database
salsaEvent.find({}, function(err, salsaevents){
if (err){
console.log(err);
} else {
res.render("salsaevents", {salsaevents: salsaevents});
}
});
});
app.post("/salsaevents", function(req, res){
// Get data from form and add to campgrounds array
var name = req.body.name;
var image = req.body.image;
var newsalsaevent = {name: name, image: image};
// Create a new campground and save to database
salsaEvent.create(newsalsaevent, function(err, newlyCreated){
if (err){
console.log("There was an error");
} else {
res.redirect("/salsaevents");
}
});
});
app.get("/salsaevents/new", function(req, res){
res.render("new");
});
// SHOW - show a single campground
app.get("/salsaevents/:id", function(req, res){
// Find the salsa event with provided ID
// Render show template with that salsa event
res.render("show");
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Server is listening...!");
});