-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
33 lines (28 loc) · 1 KB
/
example.js
File metadata and controls
33 lines (28 loc) · 1 KB
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
var http = require('http');
var urllib = require('url');
var datastore = [];
function okWithJSON(res, data) {
res.writeHead(200, {'Content-type':'application/json'});
res.end(JSON.stringify(data));
}
http.createServer(
function (req, res) {
var url = urllib.parse(req.url);
if (req.url == '/tasks' && req.method == 'POST') {
req.addListener("data", function(data) {
var p = data.toString().split('=');
if (p[0] == "title") {
datastore.push(p[1]);
}
});
req.addListener("end", function(data) {
okWithJSON(res, {})
});
} else if (req.url == '/tasks' && req.method == 'GET') {
okWithJSON(res, datastore);
} else if (url.pathname == '/tasks' && req.method == 'PUT') {
url.query
okWithJSON(res, datastore);
} //sdf
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');