-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (80 loc) · 1.85 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Main(Entry) Application
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
//pug code pretty
app.locals.pretty = true;
//pug (jade)
app.set('view engine', 'pug');
app.set('views', './views');
//static file dir
app.use(express.static('public'));
//bodyParser (post method)
app.use(bodyParser.urlencoded({extended:false}));
//router
//get 방식
app.get('/', function(req, res) {
res.send('Welcome 2da\'s blog');
});
app.get('/form', function(req, res) {
res.render('form');
});
//post
app.post('/form_receiver', function(req, res) {
var title = req.body.title;
var description = req.body.description;
res.send(title + ',' + description);
});
//Semantic URL
app.get('/topic/:id', function(req, res){
var topics = [
'Javascript is...',
'Nodejs is...',
'Express is...'
];
var output = `
<a href="/topic/0">JavaScript</a><br>
<a href="/topic/1">Nodejs</a><br>
<a href="/topic/2">Express</a><br><br>
${topics[req.params.id]}
`;
res.send(output);
});
app.get('/topic/:id/:mode', function(req, res){
res.send(req.params.id + ',' + req.params.mode);
});
app.get('/template', function(req, res) {
res.render('temp', {time:Date(), _title:'Jade'});
});
app.get('/dynamic', function (req, res) {
var time = Date();
var lis = '';
for(var i = 0; i < 5; i++) {
lis = lis + '<li>coding</li>';
}
var output = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
Hello, Dynamic!
<ul>
${lis}
</ul>
${time}
</body>
</html>`;
res.send(output);
});
app.get('/login', function(req, res){
res.send('Login please');
});
app.get('/route', function (req, res) {
res.send('Hello Router, <img src="/2da.png">');
});
app.listen(3000, function() {
console.log('Connected 3000 port!');
});