-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (65 loc) · 2.01 KB
/
Copy pathapp.js
File metadata and controls
75 lines (65 loc) · 2.01 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
66
67
68
69
70
71
72
73
74
75
const express = require('express')
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//setup public folder
app.use(express.static('./public'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.render('pages/home')
});
app.get('/links', function(req, res) {
//array with items to send
const items = [
{ name: 'node.js', url: 'https://nodejs.org/en/' },
{ name: 'ejs', url: 'https://ejs.co' },
{ name: 'expressjs', url: 'https://expressjs.com' },
{ name: 'reactjs', url: 'https://reactjs.org/' },
{ name: 'mongodb', url: 'https://www.mongodb.com/' }
];
res.render('pages/links', {
links: items
})
});
app.get('/list', function(req, res) {
//array with items to send
const items = ['node.js', 'expressjs', 'ejs', 'javascript', 'bootstarp'];
res.render('pages/list', {
list: items
})
});
app.get('/table', function(req, res) {
//array with items to send
const items = [
{ name: 'node.js', url: 'https://nodejs.org/en/' },
{ name: 'ejs', url: 'https://ejs.co' },
{ name: 'expressjs', url: 'https://expressjs.com' },
{ name: 'reactj', url: 'https://reactjs.org/' },
{ name: 'mongodb', url: 'https://www.mongodb.com/' }
];
res.render('pages/table', {
table: items
})
});
//our alert message midleware
function messages(req, res, next) {
let message;
res.locals.message = message;
next();
}
app.get('/form', messages, function(req, res) {
res.render('pages/form');
});
app.post('/form', function(req, res) {
const message = req.body;
res.locals.message = message;
res.render('pages/form');
});
app.listen(port, () => console.log(`App Started on port ${port}!`));