Simple http middleware for serving PHP files
npm install http-php
npm i http-php
`
You also need PHP-CGI to be already installed and preferably configured in the PATH.
Usage
First of all, add module with:
`js
const php = require('http-php');
`
Then, create file compiler:
`js
const file_php = php('path/to/file.php');
`
or
`js
const file_php = php({
file: 'path/to/file.php', // Path to PHP file
php: 'php-cgi', // (Optional) Path to PHP compiler
cwd: './', // (Optional) Current working directory
abort: someAbortSignal, // (Optional) AbortSignal which can stop compilation process
timeout: 500, // (Optional) In milliseconds the maximum amount of time the process is allowed to run
env: { // (Optional) PHP Environment variables
ARGS: JSON.stringify({
arg1: 'Ohayo ',
arg2: 'Sekai!'
})
// ... there are more variables explained in JSDoc
}
});
`
Compiler accepts request and optionally response parameters and returns compiled php file data as a promise.
Examples
$3
`js
const file_php = require('http-php')({
file: 'path/to/file.php',
timeout: 1000,
env: {
REDIRECT_STATUS: 300
}
});
require('express')
.all('/path', async (req, res, next) => {
let { body: page } = await file_php(req).catch(next); // Returns compiler output as a string
// Do something with page
page = page.replace('Old Title ', 'New Title ');
res.send(page);
})
.listen(80);
`
You can also simplify router declaration by one-lining it:
`js
require('express')()
.all('/path', require('http-php')('path/to/file.php'))
.listen(80);
`
This shortcut is useful when you don't need to change content of the compiled page.
$3
`js
require('http')
.createServer(require('http-php')('path/to/file.php'))
.listen(80);
``