-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
81 lines (69 loc) · 2.28 KB
/
main.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
var estatico = require('node-static'),
http = require('http');
var webroot = '.';
var file = new(estatico.Server)(webroot, {
cache: 600,
headers: { 'X-Powered-By': 'node-static' }
});
var fs = require('fs');
String.prototype.regexIndexOf = function(regex, startpos) {
var indexOf = this.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
function replaceInclude(string, context) {
var token = '<?php';
var beginPhpPos = string.indexOf(token);
if (beginPhpPos > -1) {
var before = string.substring(0, beginPhpPos);
var after = string.substring(beginPhpPos);
var endPhpPos = after.indexOf('?>');
var phpCode = after.substring(0,endPhpPos);
var after = after.substring(endPhpPos+2);
var includePos = phpCode.indexOf('include');
if (includePos > -1) {
var startPath = phpCode.indexOf("'");
var endPath = phpCode.lastIndexOf("'");
var path = phpCode.slice(startPath + 1, endPath);
console.log("Including: " + path);
var includeHtml = fs.readFileSync(context + '/' + path);
return before + includeHtml + replaceInclude(after, context);
}
}
return string;
}
function pathDeep(path) {
var pos = path.indexOf('/');
if (pos == -1) {
return 0;
} else {
return 1 + pathDeep(path.substring(pos+1));
}
}
// create a server
http.createServer(function(req, res) {
//console.log(req);
var posViews = req.url.indexOf(".php");
if (posViews > -1) {
console.log("Sending DYNAMIC file: " + req.url.substring(1));
var data = fs.readFileSync(req.url.substring(1));
var string = data.toString();
var posName = req.url.lastIndexOf('/');
var context = req.url.substring(1,posName);
//Include files directive
string = replaceInclude(string, context);
//Write
res.end(string);
} else {
req.addListener('end', function() {
file.serve(req, res, function(err, result) {
if (err) {
console.error('Error serving %s - %s', req.url, err.message);
res.writeHead(err.status, err.headers);
res.end();
} else {
console.log('Sending static file: %s', req.url);
}
});
});
}
}).listen(8080);