Can anyone please guide me through to implement PHP as template for NodeJS application. I am planning to use : PHP to load only the front-end html template (security purpose) ExpressJs as my backend.
I got struck in implementing the same here i am attaching the code below :
var phpnode = require("php-node");
app.engine('php', phpnode);
app.set('view engine', 'php');
and I am trying to load template like this :
router.get('/index.php', function(req, res, next) {
res.render('index.php');
});
Minimal example:
File structure:
./test.js:
var express = require('express'),
app = express(),
//make sure that `bin` location is right for you system
phpnode = require('php-node')({bin:"/usr/local/bin/php"});
app.set('views', __dirname+'/php/');
app.engine('php', phpnode);
app.set('view engine', 'php');
app.all('/phpinfo.php', function(req, res) {
res.render('phpinfo');
})
var port = process.env.PORT || 8081;
app.listen(port, function() {
console.log("Listening on " + port);
})
./php/phpinfo.php
<?php phpinfo(); ?>
The open http://127.0.0.1:8081/phpinfo.php in your browser, you should see phpinfo text output (not a HTML, plain text, because php we ran in a CLI mode, exactly like if you ran it in terminal)
Tell me if you need it not in a CLI mode, I'll try to find out how to switch it..