-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_short.js
116 lines (104 loc) · 3.38 KB
/
url_short.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var sys = require('sys'),
fs = require('fs'),
querystring = require('querystring'),
http = require('http');
var parse = require('url').parse;
var mongodb = require('./node-mongodb-native/lib/mongodb');
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : mongodb.Connection.DEFAULT_PORT;
var db = new mongodb.Db('short_url', new mongodb.Server(host, port, {}), {native_parser:true});
db.open(function(err, db)
{
http.createServer(function (req, res) {
// work out which file is being requested
var path = parse(req.url).pathname;
if(path.match(/\/$/)) {
// assume index.html
path += "index.html";
}
// make path relative to "."
path = "."+path;
/* Click 'Shorten' button. */
if (req.method == 'POST') {
/* Save url map info. */
post_handler(req, function(req_data)
{
db.collection('urlmaps', function(error, collection)
{
collection.find({'short' : req_data.short_url, 'long': req_data.long_url}).toArray(function(err, docs)
{
/* Not exist. */
if (docs.length == 0) {
/* Save url map info in db. */
collection.insert({'short': req_data.short_url, 'long' : req_data.long_url}, {safe:true},
function(err, objects) { if (err) console.warn(err.message); });
} else {
collection.update({'short':req_data.short_url}, {'short': req_data.short_url, 'long' : req_data.long_url}, {safe:true},
function(err, objects) { if (err) console.warn(err.message); });
}
});
});
});
}
// load the file
fs.readFile(path, function (err, data) {
if(!err) {
// extract file type
var filetype=path.match(/\.[a-zA-Z]+$/)[0];
// work out the mime type
var ct;
switch(filetype) {
case ".htm":
case ".html": ct="text/html"; break;
case ".js": ct="text/javascript"; break;
case ".css": ct="text/css"; break;
case ".png": ct="image/png"; break;
case ".jpg": ct="image/jpg"; break;
case ".gif": ct="image/gif"; break;
default: ct = "text/plain";
}
// HTTP 200 header
res.writeHead(200, {'Content-Type': ct});
// and end the connection with the contents of the static file
return res.end(data);
} else {
db.collection('urlmaps', function(error, collection)
{
collection.find({'short' : path.substr(2)}).toArray(function(err, docs)
{
if (docs.length != 0) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(
'<html><head><script type="text/javascript">' +
'window.location = "' + docs[0].long + '";' +
'</script></head><body><body></html>'
);
return res.end();
} else {
// sorry 404
res.writeHead(404, {'Content-Type': 'text/plain'});
return res.end('URL not found\n');
}
});
});
}
});
}).listen(8000);
});
function post_handler(request, callback)
{
var _REQUEST = { };
var _CONTENT = '';
if (request.method == 'POST')
{
request.addListener('data', function(chunk)
{
_CONTENT+= chunk;
});
request.addListener('end', function()
{
_REQUEST = querystring.parse(_CONTENT);
callback(_REQUEST);
});
};
};