-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (69 loc) · 1.93 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
// Init server
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
// Init db
var db = require('./data/config.js');
var Sequence = require('./data/sequenceModel.js');
var PORT = process.env.PORT || 3000;
// Middleware
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// Routes
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/home.html');
});
app.get('/new', function(req, res) {
var seq = new Sequence();
seq.save(function(err, seqEntry) {
if(seqEntry) {
res.redirect('/draw/' + seqEntry.id);
} else {
res.send('Error creating new sequence.');
}
});
})
// Figure out rerouting logic here
app.use('/draw', express.static(__dirname + '/public'));
app.get('/draw/:id', function(req, res) {
var id = req.params.id;
Sequence.findOne({_id: id}, function(err, seqEntry) {
if(err) {
res.redirect('/');
} else {
res.sendFile(__dirname + '/public/draw.html');
}
});
});
app.get('/data/:id', function(req, res) {
var id = req.params.id;
Sequence.findOne({_id: id}, function(err, seqEntry) {
if(err) {
res.redirect('/');
} else {
res.status(200).send(seqEntry);
}
});
});
app.post('/data/:id', function(req, res) {
var id = req.params.id;
var seqData = req.body;
Sequence.findOneAndUpdate({_id: id}, seqData, {new: true}, function(err, seqEntry) {
if(err) {
res.status(400).send({
error: err,
message: 'Unable to update.'
});
} else {
res.status(200).send(seqEntry);
}
});
});
// Everything else, route back to homepage.
app.get('/*', function(req, res) {
res.redirect('/');
})
app.listen(PORT, function () {
console.log('drawDNA server listening on port', PORT, 'at', new Date().toLocaleTimeString());
});