-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
85 lines (76 loc) · 2.4 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
var express = require('express');
var fs = require('fs');
var harp = require('harp');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var browserSync = require('browser-sync');
var connectbs = require('connect-browser-sync');
var app = express();
app.set('views', './structure-views');
app.set('view engine', 'jade');
var bs = browserSync({
logSnippet: false,
files: [
__dirname + '/public/**/*.jade',
__dirname + '/public/**/*.json',
__dirname + '/public/**/*.coffee',
__dirname + '/public/**/*.ejs',
__dirname + '/public/**/*.less',
__dirname + '/public/**/*.md',
__dirname + '/public/**/*.styl',
__dirname + '/public/**/*.scss',
__dirname + '/public/**/*.sass',
__dirname + '/public/**/*.js'
]
});
app.use(connectbs(bs));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(harp.mount(path.join(__dirname, 'public')));
app.get('/', function(req, res, next) {
res.redirect('/site');
});
app.get('*/edit-json', function (req, res) {
var dataJsonPath = path.dirname(req.originalUrl),
dataJsonFolder = __dirname + '/public' + dataJsonPath,
dataJsonFilePath = dataJsonFolder + '/_data.json';
fs.exists(dataJsonFilePath, function (exists) {
console.log(exists);
if (exists) {
fs.readFile(dataJsonFilePath, 'utf8', function(err, data) {
res.render('edit', {
jsonContent: JSON.parse(data),
link: dataJsonPath
});
});
} else {
res.redirect('/site');
}
});
});
app.post('*/edit-json', function (req, res) {
var dataJsonFilePath = __dirname + '/public' + path.dirname(req.originalUrl) + '/_data.json';
fs.exists(dataJsonFilePath, function (exists) {
console.log(exists);
if (exists) {
fs.writeFile(dataJsonFilePath, JSON.stringify(JSON.parse(req.body.jsonFile), null, 4), function(err) {
if(err) {
res.sendStatus(500);
res.send('Could not save file.');
console.log('Could not save file.', err);
} else {
res.sendStatus(200);
console.log('File saved: /public/data.json');
}
});
} else {
res.sendStatus(500);
res.send('File does not exist.');
console.log('File does not exist.', err);
}
});
});
module.exports = app;