-
Notifications
You must be signed in to change notification settings - Fork 4
/
example-stream.js
61 lines (55 loc) · 1.83 KB
/
example-stream.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
var sys = require('sys');
var http = require('http');
var url = require('url');
//var repl = require('repl');
var nodemachine = require('./lib/nodemachine');
var serverPort = 8080;
function MyApp () {
}
sys.inherits(MyApp, nodemachine.App);
MyApp.prototype.canHandleResource = function MyApp__canHandleResource (context) {
return(true);
}
MyApp.prototype.resourceExists = function MyApp__resourceExists (context, callback) {
callback(context, true);
}
MyApp.prototype.resourceEtag = function MyApp__resourceEtag (context, callback) {
callback(context, "ajksdhasjhdgajsghdjhags");
}
MyApp.prototype.resourceModified = function MyApp__resourceModified (context, callback) {
callback(context, new Date());
}
MyApp.prototype.contentTypesProvided = function MyApp__contentTypesProvided (context) {
return(["text/plain"]);
}
MyApp.prototype.resourceExpiration = function MyApp__resourceExpiration (context, callback) {
var exp = new Date();
exp.setTime(exp.getTime() + 3600000);
callback(context, exp);
}
MyApp.prototype.getResource = function MyApp__getResource (context, callback) {
callback(context, true);
}
MyApp.prototype.completeResponse = function MyApp__completeResponse (context, callback) {
context.req.uri = url.parse(context.req.url);
var limit = parseInt(context.req.uri.pathname.replace('/', ''));
function sendLoop (context, limit) {
context.res.sendBody("Ping: " + limit + "\n");
if (limit <= 0)
callback(context, true);
else
setTimeout(function () {
sendLoop(context, limit - 1);
}, 1000);
}
sendLoop(context, limit);
}
MyApp.prototype.bufferResponse = function MyApp__bufferResponse (context, callback) {
return(false);
}
var server = nodemachine.createServer(8080);
server.trace = true;
server.addApp(new MyApp());
server.start();
sys.puts("Ready for requests @ http://localhost:" + serverPort + "/5");
//repl.start();