Server Part1

Welcome to The One Minute Server

The Idea

The idea is to build something with node.js.
I decided to stored objects, just for one minute. What can you do with such a server ?
Let´s discover it.

ArchitectureOverview

Overview

Setup

You need a node-server running. And you can send Get-requests to the
server with objects in the url. e.g. : http://www.server.com/img/l=Base64Coded-Entity.
And you can get available data from the server: http://www.server.com/getdata

Server

1
2
3
4
5
6
7
8
9
10
11
12
13
// most important parts
var app = express();
app.locals.datastore = new Array();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'applications')));
app.use('/', index);
app.use('/img', img);
app.use('/getdata', getdata);

Why “img” as path ? Because the first example dependes on
web-tracking, and it is so that you request a pixel from the server with
your tracking-request for most tracking solutions. So i went the same way.

The Img.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
var express = require('express');
var router = express.Router();
const fs = require('fs');
var url = require('url');
var image = fs.readFileSync('/var/www/html/1px.png');
//******************************************************************************
router.get('/*', function(req, res, next) {
var datum = new Date();
var currentTime = datum.getTime() ;
var deleteTime = currentTime - 60000;
var o = parseUrl(req.url, currentTime ); // get Information from Url to Json
storeLocal(o,req); // store data in local array
cleanUp(deleteTime,req); // delete old data
res.end(image, 'binary');
});
//******************************************************************************
function parseUrl( requrl, currentTime ){
var url_parts = url.parse(requrl, true);
var query = url_parts.query;
var b = new Buffer(""+query.l, 'base64');
var o = JSON.parse( b.toString());
o.timestamp = currentTime;
o.pkey = ""+currentTime+"_"+o.clientid;
return o;
}
//******************************************************************************
function cleanUp(cleanupTime, req){
var temp = [];
var foundAt =-1;
for( index in req.app.locals.datastore){
var objtime = req.app.locals.datastore[index].timestamp;
if(objtime < cleanupTime){
foundAt = index;
}
}
if(foundAt > -1){
req.app.locals.datastore = req.app.locals.datastore.slice(foundAt);
}
}
//******************************************************************************
function storeLocal(obj,req){
req.app.locals.datastore.push(obj);
}
//******************************************************************************

I build a small data-transfer-library which you can integrate in any website to send some values. The lib has one function which is “track(JSON-Object)”. More about this in the next posts.