-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
86 lines (73 loc) · 2.22 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
const express = require('express');
const socket = require('socket.io');
const mongoose = require('mongoose');
const fs = require('fs');
const app = express();
const port = 3000;
const server = app.listen(port, () => console.log(`\nlistening on port: ${port}`));
app.use(express.static('public'));
app.use(express.json({limit: '1mb'}));
//DB
mongoose.connect('mongodb+srv://admin:[email protected]/canvas?retryWrites=true&w=majority', {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('\nDB Connected'))
.catch(err => {
console.log('\nDB Connection Error: ${err.message}');
});
var Schema = mongoose.Schema;
var pathSchema = new Schema({
id: String,
pathData: String
});
var pathModel = mongoose.model('paths', pathSchema);
const io = socket(server);
io.sockets.on('connection', newConnection);
function newConnection(socket){
console.log(`\nNew connection: ${socket.id}`);
socket.on('mouseDown',function(data){
socket.broadcast.emit('mouseDown', data);
});
socket.on('mouseDrag', function(data){
socket.broadcast.emit('mouseDrag', data);
});
}
//ROUTES
app.get('/paper', function(req, res){
res.type('application/javascript');
res.sendFile(__dirname + '/node_modules/paper/dist/paper-full.min.js');
});
app.get('/p5', function(req, res){
res.type('application/javascript');
res.sendFile(__dirname + '/node_modules/p5/lib/p5.min.js');
});
app.post('/newPath', function(req, res){
let newPathEntry = new pathModel();
newPathEntry.id = req.body.id
newPathEntry.pathData = req.body.pathData;
newPathEntry.save(function(err, savedObject){
if (err){
console.log("\nError saving path to DB" + err);
res.status(500).send();
}else{
console.log('New path saved to DB.');
res.send(savedObject);
}
});
});
app.get('/getData', function(req, res){
pathModel.find({},'-_id pathData',function(err, docs){
if(err) {
console.log(err);
}else{
res.send(docs);
}
});
});
app.get('/getStory',function(req, res){
let rawdata = fs.readFileSync(__dirname + "/public/story/json.json");
let stories = (JSON.parse(rawdata)).stories;
let random = Math.floor(Math.random() * Math.floor(stories.length));
res.send(stories[random]);
})