-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (63 loc) · 1.93 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
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
//Require Node modules
var express = require('express'),
mongoose = require('mongoose'),
methodOverride = require('method-override');
//Require models
var Argument = require('./models/argument');
//Connect to database
mongoose.connect('mongodb://localhost/arguments');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log("Database connected");
});
//Basic app configuration
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(__dirname + '/public'));
app.use(methodOverride('_method'));
//Routes
app.get('/', (req, res) => {
res.render("index", { title: "World of Ideas" });
});
app.get('/workbench', (req, res) => {
Argument.find({}, (err, result) => {
if (err) console.log(err);
res.render("workbench", { arguments: result, title: "Argument Workbench" });
});
});
app.get('/profile', (req, res) => {
res.render("profile", { title: "My Ideas" });
});
app.get('/argument/:id', (req, res) => {
Argument.findOne({ _id: req.params.id }, (err, result) => {
res.render("argument", { argument: result, title: "Argument Workbench" });
});
});
app.post('/argument', (req, res) => {
Argument.create({
title: req.body.title,
premises: req.body.premises,
conclusion: req.body.conclusion
}, (err) => {
if (err) console.log(err);
res.redirect('/workbench');
});
});
app.put('argument/:id', (req, res) => {
Argument.find({ _id: req.params._id }, (err, result) => {
});
});
app.delete('/argument/:id', (req, res) => {
Argument.deleteOne({ _id: req.params.id }, (err) => {
if (err) console.log(err);
res.redirect('/workbench');
});
});
//Start server
app.listen(port, () => {
console.log("Server running on port " + port);
});