fnobi / php-express

enable express server to exec php.
36 stars 15 forks source link

php-express downloads the page instead of rendering it #15

Open avvie opened 7 years ago

avvie commented 7 years ago

basically i copy pasted you example and changed the directories, but when i request the page i get a download
php is the path

const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const submission = require('./models/submission');
const cors = require('cors');

const app = express();

var phpExpress = require('php-express')({

    // assumes php is in your PATH
    binPath: 'php'
});

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

// routing all .php file to php-express
app.all(/.+\.php$/, phpExpress.router);
// Parsers for POST data
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: false, limit: '10mb' }));

// Point static path to dist
app.use(express.static(path.join(__dirname, 'end-user/dist')));

app.get('*', (req, res) => {
    console.log('HAllo?!');
    res.sendFile(path.join(__dirname, '/end-user/dist/index.php'));
});

console.log(__dirname);
/**
 * Get port from environment and store in Express.
 */
const port = process.env.PORT || '16484';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));
7iomka commented 4 years ago

this happens after you have assigned a folder to static files as folder from your php file is located. I still do not have a workaround

tarlinux commented 3 years ago

I know it's late, but people still can use this help, php files can't be in static folders otherwise the browser will try to download them, to solve this i created a folder inside my views folder named static to hold my static files, then i used this line: app.use('/static', express.static(__dirname, 'views', 'static'); after this, all static files that are in views/static can be requested from host:port/static/

Kinuseka commented 2 years ago

use res.render() instead of res.sendFile()