jeremye
jeremye

Reputation: 1388

Can't Process PHP files with Node.js

I have a Node.js web app that I'm running on a Heroku server. I have an AJAX request in my javascript that sends a GET request to a PHP file on the server. That request is working fine, in fact, it works perfectly if I run it without any Node.js, and just a PHP web app, so I'm wondering what changes when I start with an index.js file, and node, instead of an index.php file and apache. For the sake of trying to figure this out, I have set the entire root directory as a public folder.

My index.js file is as follows:

var path = require('path');
var express = require('express');
var app = express();
var router = express.Router();
var phpExpress = require('php-express')({
    binPath: 'php'
});
var bodyParser = require('body-parser');
app.use(bodyParser.json());

app.set('port', (process.env.PORT || 5000));

app.use('/', express.static(__dirname));

app.set('views', path.join(__dirname, '/views'));
app.engine('php', phpExpress.engine);
app.set('view engine', 'php');

app.all(/.+\.php$/, phpExpress.router);

// Homepage (THIS IS NOT WHAT ISN'T WORKING)
router.get('/', function(req, res, next){
    res.render('index.php');
});
app.use('/', router);

app.use(function (req, res, next) {
    res.status(404).send("Sorry can't find that!")
});

app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});

In my browser javascript I have a simple GET request like this:

let xhttp = new XMLHttpRequest();
// THIS RETURNS THE ENTIRE CONTENTS OF THE PHP FILE, IT IS NOT PROCESSED
xhttp.open('GET', 'folder/file.php?name=' + $name, true);
xhttp.send();

Which works perfectly if I don't use Node.js, but returns the contents of the file.php, using Node.js. Chrome tries to download the file.

I've never used PHP before, and I've only made a node.js webapp that was exclusively JS on the server, so it's very possible my index.js isn't configured appropriately for PHP and/or I'm missing some other important files in my root directory; let me know if there is more information I can include.

What's going on that causes this? Any help or advice would be much appreciated, thanks :)

Addition:

I'm adding this example to be more clear:

I have a php file on my server, file.php

<?php
    $name = $_GET['name'];
    echo $name;
?>

I have a js file to run in browser, main.js

let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
        console.log(this.responseText);
    }
}
xhttp.open('GET', 'folder/file.php?name=jerry', true);
xhttp.send();

When that js file runs, it logs:

<?php
    $name = $_GET['name'];
    echo $name;
?>

But I'd like it to log:

jerry

Upvotes: 0

Views: 6006

Answers (2)

Ezzat
Ezzat

Reputation: 911

You Should Put your php folders & files inside the views folder because you set template engine path to /views that means you should have your file.php under /views/folder/file.php

This is my app structure

├── app.js
├── package.json
└── views
    └── folder
        └── file.php

and my app.js is

var path = require('path');
var express = require('express');
var app = express();
var router = express.Router();
var phpExpress = require('php-express')({
    binPath: 'php'
});
var bodyParser = require('body-parser');
app.use(bodyParser.json());

app.set('port', (process.env.PORT || 5000));

app.use('/', express.static(__dirname));

app.set('views', path.join(__dirname, '/views'));
app.engine('php', phpExpress.engine);
app.set('view engine', 'php');

app.all(/.+\.php$/, phpExpress.router);

app.use(function (req, res, next) {
    res.status(404).send("Sorry can't find that!")
});

app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});

and file.php is the same as yours

<?php
    $name = $_GET['name'];
    echo $name;
?>

You can see that you don't need to render php file by yourself , because phpExpress.router will take care of all the routes with .php extensions and tries to find these files inside views folder.

Upvotes: 1

Mekicha
Mekicha

Reputation: 851

The res.render() function Renders a view and sends the rendered HTML string to the client. Node is therefore treating the file you are sending as a template file to be rendered to the view. You should try res.sendFile instead.

update: I think you need to expose a route in your index.js that sends the file(that is, makes the file available to an external script). And then you can send a GET request to that url to get the file.

Upvotes: 0

Related Questions