Skip to content

Instantly share code, notes, and snippets.

@Markavian
Created September 7, 2018 21:06
Show Gist options
  • Save Markavian/ff100dcba4f4580ec87865c3d12a1240 to your computer and use it in GitHub Desktop.
Save Markavian/ff100dcba4f4580ec87865c3d12a1240 to your computer and use it in GitHub Desktop.
Using express and php-express to proxy requests to a PHP router
<?php
// Add decision logic based on $_SERVER['REQUEST_URI']
print "<pre>Request URI: " . $_SERVER['REQUEST_URI'] . "</pre>";
print "<pre>Request Method: " . $_SERVER['REQUEST_METHOD'] . "</pre>";
print "<pre>GET: " . print_r($_GET, true) . "</pre>";
print "<pre>POST: " . print_r($_POST, true) . "</pre>";
const express = require('express')
const path = require('path')
const app = express()
const phpExpress = require('php-express')({
binPath: 'php' // assumes php is in your PATH
})
app.get('/*', render)
function render(req, res) {
phpExpress.engine(path.join(__dirname, 'render.php'), {
method: req.method,
get: req.query,
post: req.body,
server: {
REQUEST_URI: req.url
}
}, (err, body) => {
if (err) {
res.status(500).send(err)
} else {
res.send(body)
}
})
}
const server = app.listen(3000, function () {
const port = server.address().port
console.log('PHP Express server listening at http://%s:%s', 'localhost', port);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment