Skip to content

Commit da1f5ac

Browse files
committed
first commit
0 parents  commit da1f5ac

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# fit-s

webserver/app.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var express = require('express')
2+
, fs = require('fs')
3+
, path = require('path')
4+
;
5+
6+
var app = express();
7+
8+
app.get('/user/:id', function(req, res) {
9+
/*
10+
1. Create a file named id in the tmp directory
11+
under the current path
12+
2. Write the current time on that file
13+
3. Return '<p>{{current path}}</p><p>{{contents of the file}}</p>'
14+
*/
15+
fs.writeFile(path.resolve(__dirname,'tmp/',req.params.id),
16+
(new Date()).toString(), 'utf8',
17+
function(err) {
18+
if (err) {
19+
res.send(err);
20+
return;
21+
}
22+
23+
fs.readFile(path.resolve(__dirname,'tmp/',req.params.id),
24+
function(err, data) {
25+
if (err) {
26+
res.send(err);
27+
return;
28+
}
29+
res.send('<p>'+__dirname + '</p>'+
30+
'<p>'+data+'</p>'
31+
);
32+
}
33+
);
34+
});
35+
36+
});
37+
38+
39+
var server = app.listen(3000, function() {
40+
var host = server.address().address;
41+
var port = server.address().port;
42+
43+
console.log('Example app listening at http://%s:%s', host, port);
44+
45+
});

webserver/hellopromise.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var pr = new Promise(function(resolve, reject) {
2+
window.setTimeout(function() {
3+
if (Math.random() < 0.5) {
4+
resolve(new Date());
5+
}
6+
else {
7+
reject('error');
8+
}
9+
}, 2000);
10+
11+
});
12+
13+
pr.then(function(data) {
14+
console.log('promise is resolved', data);
15+
});
16+
17+
pr.catch(function(err) {
18+
console.log('promise is rejected', err);
19+
});
20+
21+
console.log('waiting for promise',(new Date()), JSON.stringify(pr));

webserver/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "webserver",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Achmad Husni Thamrin",
10+
"license": "MIT",
11+
"dependencies": {
12+
"express": "^4.13.3",
13+
"promise": "^7.0.4"
14+
}
15+
}

webserver/tmp/husni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Wed Dec 16 2015 17:44:51 GMT+0900 (JST)

0 commit comments

Comments
 (0)