-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_file.js
107 lines (96 loc) · 2.6 KB
/
app_file.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//간단한 웹어플리케이션 제작
//좋지않은 방법이지만 이해만 해보자
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var storage = multer.diskStorage ({
destination : function (req , file , cd) {
cd(null, 'uploads/')
},
filename : function(req, file, cd ) {
cd(null, file.originalname)
}
})
var upload = multer({ storage : storage})
var fs = require('fs');
var app = express();
app.use(bodyParser.urlencoded({extended : false}));
app.locals.pretty = true;
//Jade태그의 줄바꿈을 도와준다.
app.use('/user', express.static('uploads'));
//사용자에게 이미지를 보여주고싶다면 !
app.set('views' , './views_file');
//템플릿 파일을 views_file에 저장
app.set('view engine' , 'jade');
//템플릿 엔진은 jade
app.get('/upload' , function (req, res){
res.render('upload');
})
app.post('/upload',upload.single('userfile'), function(req , res){
console.log(req.file);
res.send('Uploaded : ' + req.file.filename);
})
app.get('/topic/new', function (req, res){
fs.readdir('data', function(err, files){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
}
res.render('new',{topics:files});
})
})
app.get(['/topic','/topic/:id'] , function(req, res){
fs.readdir('data', function(err, files){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
}
var id = req.params.id;
if(id) {
//id값이 있을때
fs.readFile('data/'+id, 'utf8',function(err, data){
if(err)
{
console.log(err);
res.status(500).send('Internal Server Error');
}
res.render('view',{topics:files, title:id , description: data});
})
} else {
//id값이 없을 때
res.render('view', {topics : files, title:'Wercome', description:'Hello, JavaScript'});
}
})
})
/*
app.get('/topic/:id', function( req, res){
var id = req.params.id;
fs.readdir('data', function(err, files){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
}
fs.readFile('data/'+id, 'utf8',function(err, data){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
}
res.render('view',{topics:files, title:id , description: data});
})
})
})
*/
app.post('/topic' , function(req, res){
var title = req.body.title;
var description = req.body.description;
fs.writeFile('data/'+title,description, function(err){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
}
res.redirect('/topic/' + title);
});
});
app.listen(3000, function (){
console.log('Connected, 3000 Port!');
})