Skip to content

Commit 61eddf0

Browse files
author
Admin
committed
latest commit
0 parents  commit 61eddf0

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
NodePHP - run wordpress (and other php scripts) with node
2+
---------------------------------
3+
4+
With Node PHP you can leverage the speed of node js and run all of the widely available php scripts directly inside your express site.
5+
6+
To run wordpress with node js and express do this:
7+
8+
var express = require('express');
9+
var php = require("php");
10+
var path = require("path");
11+
12+
var app = express();
13+
14+
app.use("/", php.cgi("/path/to/wordpress"));
15+
16+
app.listen(9090);
17+
18+
console.log("Server listening!");
19+
20+
Dependencies
21+
------------
22+
23+
* php-cgi - you need to have the interpreter installed in order to use this extension

main.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var url = require("url");
2+
var child = require("child_process");
3+
var path = require("path");
4+
var fs = require("fs");
5+
6+
function runPHP(req, response, next, phpdir){
7+
var parts = url.parse(req.url);
8+
var query = parts.query;
9+
10+
var file = path.join(phpdir, parts.pathname);
11+
12+
if(!fs.existsSync(file)){
13+
file = path.join(phpdir, "index.php");
14+
} else if(fs.statSync(file).isDirectory()){
15+
file = path.join(file, "index.php");
16+
}
17+
18+
var pathinfo = "";
19+
var i = req.url.indexOf(".php");
20+
if(i > 0) pathinfo = parts.pathname.substring(i+4);
21+
else pathinfo = parts.pathname;
22+
23+
var env = {
24+
PATH_INFO: pathinfo, //The extra path information, as given in the requested URL. In fact, scripts can be accessed by their virtual path, followed by extra information at the end of this path. The extra information is sent in PATH_INFO.
25+
PATH_TRANSLATED: "", //The virtual-to-real mapped version of PATH_INFO.
26+
SCRIPT_NAME: parts.pathname, //The virtual path of the script being executed.
27+
SCRIPT_FILENAME: file,
28+
REQUEST_FILENAME: file, //The real path of the script being executed.
29+
SCRIPT_URI: req.url, //The full URL to the current object requested by the client.
30+
URL: req.url, //The full URI of the current request. It is made of the concatenation of SCRIPT_NAME and PATH_INFO (if available.)
31+
SCRIPT_URL: req.url,
32+
REQUEST_URI: req.url, //The original request URI sent by the client.
33+
REQUEST_METHOD: req.method, //The method used by the current request; usually set to GET or POST.
34+
QUERY_STRING: parts.query, //The information which follows the ? character in the requested URL.
35+
CONTENT_TYPE: "application/x-www-form-urlencoded", //The MIME type of the request body; set only for POST or PUT requests.
36+
CONTENT_LENGTH: req.rawBody.length||0, //The length in bytes of the request body; set only for POST or PUT requests.
37+
AUTH_TYPE: "", //The authentication type if the client has authenticated itself to access the script.
38+
AUTH_USER: "",
39+
REMOTE_USER: "", //The name of the user as issued by the client when authenticating itself to access the script.
40+
ALL_HTTP: Object.keys(req.headers).map(function(x){return "HTTP_"+x.toUpperCase().replace("-", "_")+": "+req.headers[x];}).reduce(function(a, b){return a+b+"\n";}, ""), //All HTTP headers sent by the client. Headers are separated by carriage return characters (ASCII 13 - \n) and each header name is prefixed by HTTP_, transformed to upper cases, and - characters it contains are replaced by _ characters.
41+
ALL_RAW: Object.keys(req.headers).map(function(x){return x+": "+req.headers[x];}).reduce(function(a, b){return a+b+"\n";}, ""), //All HTTP headers as sent by the client in raw form. No transformation on the header names is applied.
42+
SERVER_SOFTWARE: "NodeJS", //The web server's software identity.
43+
SERVER_NAME: "localhost", //The host name or the IP address of the computer running the web server as given in the requested URL.
44+
SERVER_ADDR: "127.0.0.1", //The IP address of the computer running the web server.
45+
SERVER_PORT: 8011, //The port to which the request was sent.
46+
GATEWAY_INTERFACE: "CGI/1.1", //The CGI Specification version supported by the web server; always set to CGI/1.1.
47+
SERVER_PROTOCOL: "", //The HTTP protocol version used by the current request.
48+
REMOTE_ADDR: "", //The IP address of the computer that sent the request.
49+
REMOTE_PORT: "", //The port from which the request was sent.
50+
DOCUMENT_ROOT: "", //The absolute path of the web site files. It has the same value as Documents Path.
51+
INSTANCE_ID: "", //The numerical identifier of the host which served the request. On Abyss Web Server X1, it is always set to 1 since there is only a single host.
52+
APPL_MD_PATH: "", //The virtual path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to /.
53+
APPL_PHYSICAL_PATH: "", //The real path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to the same value as DOCUMENT_ROOT.
54+
IS_SUBREQ: "", //It is set to true if the current request is a subrequest, i.e. a request not directly invoked by a client. Otherwise, it is set to true. Subrequests are generated by the server for internal processing. XSSI includes for example result in subrequests.
55+
REDIRECT_STATUS: 1
56+
};
57+
58+
Object.keys(req.headers).map(function(x){return env["HTTP_"+x.toUpperCase().replace("-", "_")] = req.headers[x];});
59+
60+
//console.log(env);
61+
//console.log("ALL: "+env.ALL_HTTP);
62+
//console.log("GET: "+file);
63+
//console.log("RAW BODY: "+req.rawBody);
64+
65+
if(/.*?\.php$/.test(file)){
66+
var res = "", err = "";
67+
68+
var php = child.spawn("php-cgi", [], {
69+
env: env
70+
});
71+
72+
//php.stdin.resume();
73+
php.stdin.write(req.rawBody+"\n");
74+
php.stdin.end();
75+
76+
php.stdout.on("data", function(data){
77+
//console.log(data.toString());
78+
res += data.toString();
79+
});
80+
php.stderr.on("data", function(data){
81+
err += err.toString();
82+
});
83+
php.on("error", function(err){
84+
console.error(err);
85+
});
86+
php.on("exit", function(){
87+
// extract headers
88+
var lines = res.split("\r\n");
89+
var line = 0;
90+
var html = "";
91+
if(lines.length){
92+
do {
93+
var m = lines[line].split(": ");
94+
if(m[0] === "") break;
95+
96+
//console.log("HEADER: "+m[0]+": "+m[1]);
97+
if(m[0] == "Status"){
98+
response.statusCode = parseInt(m[1]);
99+
}
100+
if(m.length == 2){
101+
response.setHeader(m[0], m[1]);
102+
}
103+
line++;
104+
} while(lines[line] !== "");
105+
html = lines.splice(line+1).join("\n");
106+
} else {
107+
html = res;
108+
}
109+
//console.log("STATUS: "+response.statusCode);
110+
111+
response.send(html, response.statusCode);
112+
response.end();
113+
});
114+
115+
} else {
116+
response.sendfile(file);
117+
//response.end();
118+
next();
119+
}
120+
}
121+
122+
exports.cgi = function(phproot){
123+
return function(req, res, next){
124+
var data = '';
125+
req.setEncoding('utf8');
126+
req.on('data', function(chunk) {
127+
data += chunk;
128+
});
129+
req.on('end', function() {
130+
req.rawBody = data;
131+
runPHP(req, res, next, phproot);
132+
});
133+
}
134+
}
135+

node_php_small.jpg

47.3 KB
Loading

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"author": {
3+
"name": "Martin K. Schröder"
4+
},
5+
"name": "php",
6+
"description": "Node JS modules for running wordpress in node js",
7+
"version": "0.0.1",
8+
"repository": {
9+
"url": "https://github.com/mkschreder/siteboot_php.git"
10+
},
11+
"main": "./main.js",
12+
"bin": {},
13+
"dependencies": {
14+
},
15+
"devDependencies": {
16+
"cluster": "*",
17+
"express": "*",
18+
"request": "*"
19+
},
20+
"optionalDependencies": {},
21+
"engines": {
22+
"node": "*"
23+
},
24+
"scripts": {
25+
"test": "nodeunit tests"
26+
}
27+
}

tests/express.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var express = require('express');
2+
var php = require("php");
3+
var path = require("path");
4+
var cluster = require("cluster");
5+
var request = require("request");
6+
7+
if(cluster.isMaster){
8+
cluster.fork().on("message", function(){
9+
process.exit();
10+
});
11+
12+
var app = express();
13+
14+
app.use("/", php.cgi(__dirname));
15+
16+
app.listen(9090);
17+
} else {
18+
request("http://localhost:9090/test.php", function(err, res){
19+
if(res.body == "Hello World!"){
20+
console.log("Success!");
21+
} else {
22+
console.error("Expected 'Hello World!', got: "+res.body);
23+
}
24+
process.send("exit");
25+
});
26+
}
27+

tests/test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo "Hello World!"; ?>

0 commit comments

Comments
 (0)