-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (43 loc) · 1.66 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
//express to run server
var express = require('express');
//start up an instance of app. This needs to go before any of the things below which are attached to the app
var app = express();
//connecting app to website folder which has index.html and sketch.js to run a website connected to this server/app
app.use(express.static('website'));
//cors for cross origin aloowance
var cors = require('cors');
app.use(cors());
//requring the node file read in system
var fs = require('fs')
//body-parser to work with json files
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// Set up the server
var server = app.listen(3000, listen);
//callback to debug
function listen(){
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
var creed;
// Read the file
var data = fs.readFileSync('website/creed.json', 'utf8');
console.log(data);
// Parse it back to object
creed = JSON.parse(data);
// console.log(cards);
// Now here we take a POST request. And create a directory for it to be sent to. This needs to be the same as the one referenced in sketch.js
app.post('/creed', example);
function example(req, res){
var creedLine = req.body.string
console.log(creedLine);
//this is coming in as an object: how do I make it just a string? or change the whole thing to a javascript object
// {string:"what i want"}
creed.push(creedLine);
var json = JSON.stringify(creed);
console.log(creed);
fs.writeFileSync('website/creed.json', json);
res.send(creed);
}