Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs, expressjs serving PHP files

Ok I've been playing around with nodejs, expressjs and socket.io to create some applications. But now im coming to the stage where i want to take things a bit further.

I have noticed a few node apps using PHP for twitter auth on their client side. When I attempt to rename my client.html file to client.php and restart the server it throws up a blank page with this

Cannot GET /

How do would serve php files or would i do twitter auto using js?

This is my NodeJS server.js

var http = require('http'),  
    express = require('express');

var app = module.exports = express.createServer();

// Configuration
app.configure(function(){
    app.use(express.static(__dirname + '/public'));
});

app.listen(1234);
console.log("server started on port :1234");
like image 394
ChrisMJ Avatar asked Jul 26 '11 21:07

ChrisMJ


People also ask

Can we use PHP and Nodejs together?

Yes, and yes. Node and Apache / PHP can co-exist on a single server. The only issue you are likely to run into is that they cannot both listen on the same port. HTTP, by default, runs on port 80 and only one process can "listen" on a single port at any one time.

Can you use Express with PHP?

Note: You can also render PHP files as view template. By default, Express PHP requires no template engine to render view, it can render, PHP file a template.

Is Nodejs faster than PHP?

Due to the V8 engine, asynchronous execution, and real-time server interaction, Node. js offers a better execution speed and certainly outperforms PHP. Node. js sends a request to the computer's file system.

How much traffic can express JS handle?

There's a benchmark made by Fastify creators, it shows that express. js can handle ~15K requests per second, and the vanilla HTTP module can handle 70K rps.


1 Answers

As already noted, node.js itself won't interpret php files for you. Available options are:

  • have nginx in front of node and reverse proxy all request you want to process in node from it
  • proxy php requests from node to php-capable server
  • spawn php process on each request in node
  • make fastcgi link to php-fastcgi using node fastcgi protocol parser
like image 175
Andrey Sidorov Avatar answered Sep 25 '22 03:09

Andrey Sidorov