-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (54 loc) · 1.5 KB
/
Copy pathindex.js
File metadata and controls
65 lines (54 loc) · 1.5 KB
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
const express = require("express")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")
var headings= []
var bodys=[]
var heading, body=""
var _ = require("lodash")
mongoose.connect("mongodb://localhost:27017/blogDB")
const app = express()
app.set("view engine", "ejs")
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static(__dirname))
const textSchema = mongoose.Schema({
heading: String,
content: String
})
const Text = mongoose.model("text", textSchema)
app.get("/", function(req,res){
Text.find({}, function(err,t){
res.render("home", {title: t, content: t})
for (var i=0;i<headings.length;i++){
var h = headings[i]
var c = bodys[i]
var iD = t[i]
app.get("/"+iD.id, function(req,res){
res.render("extra", {title: h, content:c})
})
}
})
})
app.get("/about",function(req,res){
res.render("about")
})
app.get("/contact",function(req,res){
res.render("contact")
})
app.get("/compose", function(req,res){
res.render("compose")
})
app.post("/compose", function(req,res){
const title = req.body.title
const body = req.body.content
const text = new Text({
heading: title,
content: body
})
text.save()
headings.push(text.heading)
bodys.push(text.content)
res.redirect("/")
})
app.listen(3000, function(){
console.log("Server 3000")
})