-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (48 loc) · 1.3 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
const express = require('express');
const mongoose = require('mongoose');
const ejs = require('ejs');
const path = require('path');
const Post = require('./models/posts')
const app = express();
const port = 3000;
// DB Connection
mongoose.set("strictQuery", false);
mongoose.connect('mongodb://localhost/cleanblog-test-db', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// TAMPLATE ENGINE
app.set('view engine', 'ejs');
// MIDDLEWARES
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true })); // url de ki datayı okumaya yarıyor
app.use(express.json()); // url deki datayı json a çevirmeye yarıyor
// ROUTERS
app.get('/', async (req, res) => {
const Posts = await Post.find({})
res.render('index',{
Posts
});
});
app.get('/about', (req, res) => {
res.render('about');
});
app.get('/post', (req, res) => {
res.render('post');
});
app.get('/addpost', (req, res) => {
res.render('add_post');
});
app.get('/posts/:id', async (req, res) => {
const Posts = await Post.findById(req.params.id)
res.render('post',{
Posts
});
});
app.post('/sendpost', async (req,res)=>{
await Post.create(req.body)
res.redirect('/'); // adresine yönlendirioruz
})
app.listen(port, () => {
console.log(`sunucu ${port}'unda açıldı.`);
});