-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (46 loc) · 1.3 KB
/
app.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
require("dotenv").config();
const express = require("express");
const ejs = require("ejs");
const mongoose = require("mongoose");
const app = express();
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
mongoose.connect(
`mongodb+srv://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER}.mongodb.net/${process.env.MONGO_DATABASE}?authSource=admin`
);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", function () {
console.log("Connected successfully");
});
const textSchema = {
content: String,
time: { type: Date, default: Date.now },
};
const Text = mongoose.model("Text", textSchema);
app.get("/", function (req, res) {
Text.find({}, function (err, texts) {
res.render("home", {
texts: texts,
});
});
});
app.post("/", function (req, res) {
const text = new Text({
content: req.body.textMessage,
});
text.save(function (err) {
if (!err) {
res.redirect("/");
}
});
console.log(text);
});
app.get("/changelog", function (req, res) {
res.render("changelog");
});
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Server has started successfully on " + port);
});