Created
July 14, 2014 06:23
-
-
Save RobinStamer/a39281e2277cfafe3c91 to your computer and use it in GitHub Desktop.
Post-Express server (WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var php = require("./node-php"); | |
| var urlp = require("url"); | |
| var path = require("path"); | |
| var options = require("./options").options; | |
| var ev = require('./ev') | |
| ,harden = require('harden') | |
| ,fs = require('fs') | |
| ,exp = require('express') | |
| ,proxy = require('http-proxy').createProxyServer() | |
| ,app = exp() | |
| var agent = new php.Agent(4, options) | |
| agent.on("error", function(err) { | |
| console.log("client.error") | |
| console.log(err) | |
| }) | |
| function errHandle(e, args) { | |
| console.log(e.stack) | |
| } | |
| function PHP(req, res) { | |
| agent.request(req, res, function(err, response) { | |
| if (err) { | |
| console.log('!!!') | |
| console.log(err) | |
| console.log(err.stack) | |
| res.end('Error!') | |
| } | |
| }) | |
| } | |
| function timestamp() { | |
| var d = new Date | |
| ,month = d.getMonth() | |
| ,day = d.getDate() | |
| ,hour = d.getHours() | |
| ,minute = d.getMinutes() | |
| if (10 > month) { | |
| month = '0' + month | |
| } | |
| if (10 > day) { | |
| day = '0' + day | |
| } | |
| if (10 > hour) { | |
| hour = '0' + hour | |
| } | |
| if (10 > minute) { | |
| minute = '0' + minute | |
| } | |
| return d.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute | |
| } | |
| app.use(function (req, res, next) { | |
| harden(req); harden(res) | |
| req.on('harden:exception', errHandle) | |
| res.on('harden:exception', errHandle) | |
| next() | |
| }) | |
| server = app.listen(options.server.port, function() { | |
| console.log('Listening on port %d', server.address().port); | |
| }) | |
| getChildName = function(req) { | |
| var s = (req.headers.host || '') | |
| ,i = s.indexOf(':') | |
| return -1 == i ? [s, 80] : [s.substr(0, i), parseInt(s.substr(i + 1))] | |
| } | |
| app.engine('php', function(path, options, cb) { | |
| console.dir(arguments) | |
| }) | |
| app.all('/*', function(req, resp, next) { | |
| var child = getChildName(req) | |
| ,host = child[0] | |
| ,stat | |
| ,dir = path.dirname(req.url) | |
| ,ext = path.extname(urlp.parse(req.url).pathname) | |
| console.log('[' + timestamp() + ']' + ext + '://' + child.join(':') + req.url + ' (' + req.connection.remoteAddress + '/' + req.headers.referer + ')') | |
| if ('.php' == ext || '/' == req.url) { | |
| PHP(req, resp) | |
| return | |
| } | |
| next() | |
| }) | |
| app.use(exp.static(__dirname)) | |
| app.all('/evlog', ev) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment