I've heared that NodeJS is great for making real-time chat applications and I want to implement a chat on my website. Right now I only have the design so I have to make the back-end code now.
However when I'm using socket.io + express
it doesn't work out like it should.
Code from server.js
:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.php');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I simply want to show index.php
on localhost:3000
, however when I browse to the website it doesn't want to show the page. Instead it downloads the index.php
file.
But if I change the extention of the file to html, so it bees index.html
, it works. But that is not what I want.
I know there are some smart guys out there that can solve this problem in no time. I've been sitting for a very long time myself trying to sovle it.
I've heared that NodeJS is great for making real-time chat applications and I want to implement a chat on my website. Right now I only have the design so I have to make the back-end code now.
However when I'm using socket.io + express
it doesn't work out like it should.
Code from server.js
:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.php');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I simply want to show index.php
on localhost:3000
, however when I browse to the website it doesn't want to show the page. Instead it downloads the index.php
file.
But if I change the extention of the file to html, so it bees index.html
, it works. But that is not what I want.
I know there are some smart guys out there that can solve this problem in no time. I've been sitting for a very long time myself trying to sovle it.
Share edited Jun 4, 2017 at 13:52 Gleb Kemarsky 10.4k7 gold badges46 silver badges71 bronze badges asked Jun 4, 2017 at 13:21 Nabil HayekNabil Hayek 1653 silver badges9 bronze badges 4- At Stack Overflow you are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a Minimal, Complete, and Verifiable example. I suggest reading How to Ask a good question and the perfect question. Also, be sure to take the tour and read this. – John Conde Commented Jun 4, 2017 at 13:21
- I've posted this to learn, not for someone to make it for me. – Nabil Hayek Commented Jun 4, 2017 at 13:26
- 1 Unfortunately that is not allowed here. This isn't a tutorial site, google, or the manual. It's for developers who have written some code and need help understanding why it isn't working as expected. – John Conde Commented Jun 4, 2017 at 13:27
- Yes, and in this case i need help understanding why it downloads the file instead of showing it. That way I can know how to proceed with the project myself. – Nabil Hayek Commented Jun 4, 2017 at 13:29
1 Answer
Reset to default 5You can use the php-express
package to render PHP files with Express:
npm install --save php-express
Then use this (make sure you have PHP installed on your machine):
server.js
var express = require('express');
var app = express();
var phpExpress = require('php-express')({
binPath: 'php'
});
// set view engine to php-express
app.set('views', './views');
app.engine('php', phpExpress.engine);
app.set('view engine', 'php');
// routing all .php file to php-express
app.all(/.+\.php$/, phpExpress.router);
var server = app.listen(3000, function () {
console.log('listening on *:3000');
});
Source: https://www.npmjs./package/php-express