-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
42 lines (34 loc) · 852 Bytes
/
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
const express = require('express')
const app = express()
const path = require('path')
const tasks = require('./tasks') //module exports
app.set('view engine', 'hbs')
app.use(express.urlencoded({
extended: true
}))
app.use(express.json())
app.get('/tasks', (req, res) => {
res.render('tasks',//file name of hbs
{
title: 'ToDos',
tasks: tasks.getTasks() //send data as object to hbs file
})
})
app.post('/tasks', (req, res) => {
if (req.body.name.trim().length > 0) {
tasks.addTask(req.body, () => {
res.redirect('/tasks')
})
} else {
res.redirect('/tasks')
}
})
app.use('/', express.static(
path.join(__dirname, 'public')
)
)
tasks.loadTask(() => {
app.listen('5050', () => {
console.log('running on http://localhost:5050')
})
})