forked from clarissalittler/backbone-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodelServer.js
61 lines (49 loc) · 1.33 KB
/
modelServer.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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : false}));
app.use(express.static(__dirname));
var texts = [];
// Allow optional test data...
var testValues = ['Zero','One','Two','Three','Four','Five...data...things!']
var useTestValues = process.argv[2];// a number, optional extra argument when starting server
if (useTestValues)
texts = testValues.slice(0,useTestValues);
function showData() {
console.log('Data store is now: ', texts);
}
app.get('/texts/:id', function (req, res) {
var id = req.params.id;
console.log('Sending text #%s...',id);
res.send({value : texts[id]});
});
/*
app.put('/texts/:id', function (req, res) {
var id = req.params.id;
console.log('Receiving text #%s...',id);
texts[id] = req.body.value;
showData();
res.send({});
});
/*
app.post('/texts', function (req, res) {
console.log('Receiving new text...');
var newid = texts.length;
console.log('Assigning id of %s',newid);
texts[newid] = req.body.value;
showData();
res.send({id:newid});
});
/*
app.get('/texts', function (req, res) {
console.log('Sending all texts...');
showData();
var textsAndIDs = texts.map(function (v, i) {
return {id : i, value : v};
});
res.send(textsAndIDs);
});
*/
app.listen(3000);
showData();