-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (43 loc) · 1.34 KB
/
server.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
const { response } = require('express')
let express = require('express')
let app = express()
let session = require('express-session')
// Moteur de templates
app.set('view engine', 'ejs')
// Midlleware
app.use('/assets', express.static('public'))
app.use(express.urlencoded({ extended: false}))
app.use(express.json())
app.use(session({
secret: 'azezeza',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}))
app.use(require('./middlewares/flash'))
// Routes
app.get('/', (request, response) => {
let Message = require('./models/message')
Message.all(function (messages) {
response.render('pages/index', {messages: messages})
})
})
app.post('/', (request, response) => {
if (request.body.message === undefined || request.body.message === '') {
request.flash('error', "Vous n'avez pas posté de message")
response.redirect('/')
} else {
let Message = require('./models/message')
Message.create(request.body.message, function () {
request.flash('success', "Merci !")
response.redirect('/')
})
}
})
app.get('/message/:id', (request, response) => {
let Message = require('./models/message')
Message.find(request.params.id, function(message) {
response.render('messages/show', {message: message})
})
})
app.listen(8080)