-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
94 lines (87 loc) · 2.98 KB
/
index.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
/* eslint no-console: 0 */
const path = require('path');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
/**
* Run an Express server embedding a simple UI for editing the content of the given dataFile.
*
* @param {Object} [options]
* @param {Object} [options.dataFile]
* A JSON file with the content. This parameter is mandatory.
* The content authored from the web site will be saved with that path name.
* The structure of this JSON file must match the model.
* See an example here: https://github.com/amelki/cms-json/blob/master/default/data.json
* @param {Object} [options.modelFile]
* A JSON schema file representing the model to support/ease authoring via the UI. This parameter is mandatory.
* See an example here: https://github.com/amelki/cms-json/blob/master/default/schema.json
* @param {Object} [options.port] The server port. 3000 by default
* @returns {*} The express app
*/
module.exports.run = function(options) {
options = options || {};
// const port = isDeveloping ? 3000 : process.env.PORT;
var port = options.port || 3000;
var modelFile = options.modelFile;
var dataFile = options.dataFile;
const isDeveloping = options.env === 'development';
const isPlayground = options.env === 'playground';
if (!modelFile) {
throw "Model file not provided";
}
if (!dataFile) {
throw "Data file not provided";
}
const app = express();
app.use(bodyParser.json());
app.use(cors());
if (!isPlayground) {
app.options('/schema.json', cors());
app.get('/schema.json', function (req, res) {
fs.readFile(modelFile, 'utf-8', (err, json) => {
if (err) throw err;
res.send(json);
});
});
app.options('/data.json', cors());
app.get('/data.json', function (req, res) {
fs.readFile(dataFile, 'utf-8', (err, json) => {
if (err) throw err;
res.send(json);
});
});
app.post('/data.json', function (req, res) {
var json = req.body;
fs.writeFile(dataFile, JSON.stringify(json, undefined, 2), function (err) {
if (err) console.log(err);
console.log("File " + dataFile + " saved");
res.send("OK");
});
});
app.post('/schema.json', function (req, res) {
var json = req.body;
fs.writeFile(modelFile, JSON.stringify(json, undefined, 2), function (err) {
if (err) console.log(err);
console.log("File " + modelFile + " saved");
res.send("OK");
});
});
}
if (isDeveloping) {
// Include webpack only if needed, so that it is not loaded from modules than only need the production version
var withWebpack = require('./with-webpack');
withWebpack(app);
} else {
app.use(express.static(__dirname + '/dist'));
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 CMS Server Listening on port %s. Open up http://0.0.0.0:%s/ in your browser.', port, port);
});
};