-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
86 lines (75 loc) · 2.05 KB
/
server.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
var fs = require("fs");
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var https = require('https');
var mkdirp = require('mkdirp');
/*
Setup for ssl. Will need to find a way to generate a self-signed cert for
the server instance (Heroku) this gets deployed to
var options = {
key : fs.readFileSync('server.key'),
cert : fs.readFileSync('server.crt')
};
*/
/*
Serve static content from the 'public' directory.
Need to figure out how to put the content there correctly...
*/
var contentDir = path.join(__dirname, 'public');
app.use('/', express.static(contentDir));
/*
Set up json processing
*/
app.use(bodyParser.json());
/*
And form parsing
*/
app.use(bodyParser.urlencoded({extended: true}));
/*
log all request bodies.
*/
app.use(function (req, res, next) {
console.log(req.body); // populated!
next()
});
/*
Get configurations
*/
app.get('/configuration/*', function (req, res) {
console.log(req.path);
console.log(__dirname);
fs.readFile( __dirname + req.path, 'utf8', function (err, data) {
console.log( data );
// Firefox will display error if this is missing
res.setHeader("Content-Type", 'application/json');
res.end( data );
});
});
/*
Save configurations
*/
app.post('/configuration/*', function(request, response){
console.log(request.body); // your JSON
mkdirp(__dirname + '/configuration', function(err) {
console.log(err);
});
var formattedToWrite = JSON.stringify(request.body, null, 4);
fs.writeFile(__dirname + request.path, formattedToWrite, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved to " + __dirname + request.path);
});
response.send(request.body); // echo the result back
});
/*
https.createServer(options, app).listen(443, function () {
console.log('Started!');
});
*/
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function() {
console.log('Server started: http://localhost:' + app.get('port') + '/');
});