-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (42 loc) · 1.18 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
var http = require('http');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan');
const PORT = process.env.PORT || 5000
var app = express();
app.use(express.static(__dirname + '/public'));
app.set("views", path.resolve(__dirname, "views"));
app.set("view engine", "ejs");
var entries = [];
app.locals.entries = entries;
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
// ROUTES
app.get("/", (req, res) => {
res.render("index");
})
app.get("/new-entry", (req, res) => {
res.render("new-entry");
})
app.post("/new-entry", (req, res) => {
if (!req.body.title || !req.body.body) {
res.status(400).send("Otsikko ja teksti vaaditaan!");
return;
}
entries.push({
title: req.body.title,
content: req.body.body,
omanimi: req.body.omanimi,
omakuva: req.body.omakuva,
published: new Date().toLocaleString()
// arvaus: req.body.arvaus,
});
res.redirect("/");
});
app.use((req, res) => {
res.status(404).render("404");
});
app.listen(PORT, () => {
console.log("Vieraskirja portissa ${PORT}");
});