-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.6 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
var express = require("express");
var app = express();
app.enable('trust proxy')
app.use(function(request, response, next) {
if (process.env.NODE_ENV != 'development' && !request.secure) {
return response.redirect("https://" + request.headers.host + request.url);
}
next();
})
app.use(express.static('public'))
var mongoose = require("mongoose");
mongoose.connect("mongodb+srv://dbUser:[email protected]/myBlog", {
useNewUrlParser: true,
useUnifiedTopology: true
})
app.use(express.urlencoded({extended: false}));
var articleSchema = new mongoose.Schema({
title: String,
description: String,
content: String,
date: String
})
var articleModel = mongoose.model("articles", articleSchema);
var newPost = (post) => {
var Post = new articleModel(post);
Post.save()
}
app.get("/sitemap.xml", (req, res) => {
res.setHeader('content-type', 'application/xml');
res.sendFile("./sitemap.xml", { root: "." })
})
app.get("/post", async (req, res) => {
const articles = await articleModel.find({}).sort({date: 1}).lean();
console.log(articles);
res.json(articles);
})
app.set('view engine', "ejs");
app.get('/', async (req, res) => {
const articles = await articleModel.find({}).sort({date: 1}).lean();
res.render("index", {articles: articles})
})
app.get("/article/:title", async (req, res) => {
const article = await articleModel.findOne({title: req.params.title.split("-").join(" ")}).lean();
if(!article) return res.send("not found");
res.render("article", {article: article})
})
app.listen(process.env.PORT || 3000, () => {
console.log(`App listening!`)
})
console.log(Date());