-
Notifications
You must be signed in to change notification settings - Fork 1
/
counter.js
27 lines (24 loc) · 955 Bytes
/
counter.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
/*
* This is a modified example of the helloworld script
* The goal here was to show in memory data structures and variables.
* Also, show basic event handlers in a manor similar to browser and
* jQuery events. The counter only increases on 'connection' events
* which for the demo I had to open a new browser or new private
* browsing tab to get a new TCP connection.
*/
var http = require('http');
// This is the basic in memory variable that will be maintained
// accross requests.
var counter = 0;
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n' + counter + ' visitors');
})
server.listen(8124, "127.0.0.1");
// This is the event handler. It showing how part of the event loop
// is working and how a basic annonymous funciton callback can be
// used.
server.on('connection', function() {
counter++;
});
console.log('Server running at http://127.0.0.1:8124/');