-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (56 loc) · 1.61 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
59
60
61
62
63
64
65
66
67
"use strict";
require("dotenv").config();
const express = require("express");
const exphbs = require("express-handlebars");
const bodyParser = require("body-parser");
const methodOverride = require("method-override");
const moment = require("moment");
const app = express();
//setup middleware to use PUT and DELETE http verbs
app.use(methodOverride("_method"));
//setup bodyParser middleware
//puts form data in request object, namely req.body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//setup handlebars as the view template engine
app.engine(
"handlebars",
exphbs({
defaultLayout: "main",
helpers: {
formatDate: (date, format) => {
//ensure date rendered is that of the browser's local time, not wherever host's server is (heroku)
return moment(date)
.utcOffset(-8)
.format(format);
},
fromDate: date => {
let start = moment(date);
let end = moment();
return start.from(end);
}
}
})
);
app.set("view engine", "handlebars");
app.use(express.static(__dirname + "/public"));
//
//load routes here
//
const ideaRoutes = require("./routes/ideaRoutes");
//anything that goes to /ideas route will use ideaRoutes.js
app.use("/ideas", ideaRoutes);
//the root route
app.get("/", (req, res) => {
res.redirect("/ideas");
});
//handle undefined routes last
app.use("*", function(req, res) {
res.redirect("/ideas");
});
//sever setup
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on localhost:${PORT}`);
});
module.exports = { app }; // for testing