-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
115 lines (89 loc) · 3.43 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const express = require('express');
const app = express();
const path = require('path');
const formidable = require('formidable')
const sd = require('silly-datetime');
const fs = require('fs');
const bodyParser = require('body-parser');
app.set('view engine', 'ejs')
app.use(express.static(path.join(process.cwd(), 'public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/upload', function (req, res) {
const form = new formidable.IncomingForm();
form.uploadDir = path.join(process.cwd(), 'public/images');
const rand = sd.format(new Date(), 'YYYYMMDDHHmm') + Math.floor((Math.random() * 10000));
form.parse(req, function (err, fields, file) {
const oldPath = file.photo.path;
const newpath = path.join(path.dirname(oldPath), rand + path.extname(file.photo.name));
fs.renameSync(oldPath, newpath);
res.redirect('/album');
});
});
app.get('/', function (req, res) {
res.render('index')
});
app.get('/blog', function (req, res) {
const stream = fs.readFileSync(path.join(process.cwd(), 'public/articles/index.json'));
const str = stream.toString();
const articles = JSON.parse(str);
res.render('blog', { articles: articles });
});
app.get('/blog/:title', function (req, res) {
const title = req.params.title;
const stream = fs.readFileSync(path.join(process.cwd(), 'public/articles/index.json'));
const str = stream.toString();
const articles = JSON.parse(str);
for(var i =0; i < articles.length; i++ ){
if(title === articles[i].title){
return res.render('article',{post:articles[i]});
}
}
});
app.delete('/article', function (req, res) {
const title = req.body.title;
const stream = fs.readFileSync(path.join(process.cwd(),'public/articles/index.json'));
const str = stream.toString();
let articles = JSON.parse(str);
articles = articles.filter(item => item.title !== title);
fs.writeFileSync(path.join(process.cwd(),'public/articles/index.json'), JSON.stringify(articles, null, 4));
res.end();
});
app.get('/write',function (req, res) {
const title = req.query.title;
console.log(title);
const stream = fs.readFileSync(path.join(process.cwd(),'public/articles/index.json'));
const str = stream.toString();
const articles = JSON.parse(str);
const article = articles.find(item => title === item.title);
res.render('write', { post: article });
});
app.post('/write', function (req, res) {
const article = req.body; //客户端传来的数据
article.author = 'yml';
article.date = '2018-08-15';
const stream = fs.readFileSync(path.join(process.cwd(), 'public/articles/index.json'));
const str = stream.toString();
let articles = JSON.parse(str);
if(article.id) {
articles = articles.map( item =>{
if (item.id === article.id) {
item.title = article.title
item.content = article.content
}
return item
}
)
} else {
articles.unshift(article);
}
fs.writeFileSync(path.join(process.cwd(), 'public/articles/index.json'),JSON.stringify(articles, null,4));
res.redirect('/blog');
});
app.get('/album', function (req, res) {
const files = fs.readdirSync(path.join(process.cwd(), 'public/images'));
res.render('album', { file: files });
});
app.listen(3000, function () {
console.log('监听3000窗口');
});