-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (48 loc) · 1.72 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
const express = require("express");
const app = express();
const expressSanitizer = require("express-sanitizer"); //remove js script from code
const methodeOverride = require("method-override");
const bodyParser = require("body-parser");
const passport = require("passport");
const LocalStrategy = require("passport-local");
const User = require("./models/user");
const mongoose = require("mongoose");
const index = require("./routes/index");
const blogs = require("./routes/blogs");
const comments = require("./routes/comments");
require("dotenv").config({ path: __dirname + "/.env" });
const url = process.env.DATABASEURL || "mongodb://localhost/restful_blog_app";
const port = process.env.PORT || 5080;
mongoose
.connect(url, { useNewUrlParser: true })
.then(() => console.log("Connected to MongoDB"))
.catch(err => console.log(err));
mongoose.set("useFindAndModify", false);
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressSanitizer()); //has to go after bodyParser
app.use(methodeOverride("_method")); //url we using in edit page is _method
app.use(
require("express-session")({
secret: "secret",
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next) {
res.locals.currentUser = req.user;
next();
});
//Routes
app.use("/", index);
app.use("/blogs", blogs);
app.use("/blogs/:id/comments", comments);
app.listen(port, process.env.IP, function() {
console.log("server started");
});