Just posted a little example of a node.js, mongolian deadbeef, express, and php working together on my Github.
Here is my app.js
<pre>var express = require('express')
, http = require('http')
, Mongolian = require("mongolian")
, ObjectId = require('mongolian').ObjectId
, Timestamp = require('mongolian').Timestamp;
var app = express();
var server = new Mongolian;
var db = server.db("nodetest");
app.configure(function(){
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.post('/address', function(req, res){
var locations = db.collection("locations");
locations.insert({
address: req.body.address,
city: req.body.city,
state: req.body.state
});
res.end();
});
app.get('/address/:id', function(req, res){
var locations = db.collection("locations");
locations.findOne({ _id: new ObjectId(req.params.id) }, function(err, post) {
post._id = post._id.toString();
res.send(JSON.stringify(post));
});
});
app.get('/addresses', function(req, res){
var locations = db.collection("locations");
locations.find().toArray(function (err, data) {
var output = [];
for(var i=0;i<data.length;i++){
data[i]._id = data[i]._id.toString();
output.push(data[i]);
}
res.send(JSON.stringify(output));
});
});
http.createServer(app).listen(3000);
console.log("Express server listening on port 3000");</pre>
Here is the php test file:
<?php
$method = $_SERVER['REQUEST_METHOD'];
$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));
switch ($method) {
case 'POST':
rest_post($request, $_POST);
break;
case 'GET':
rest_get($request);
break;
}
function rest_get($request) {
$request = implode("/", $request);
echo file_get_contents('http://localhost:3000/' . $request);
}
function rest_post($request, $_POST) {
$params = $_POST;
unset($params['submit']);
$request = implode("/", $request);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "http://localhost:3000/" . $request);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 50);
//curl_setopt($curl_handle, CURLOPT_USERPWD, "username:password");
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $params);
$buffer = curl_exec($curl_handle);
$error = curl_error($curl_handle);
curl_close($curl_handle);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mongodb node.js mongolian expressjs</title>
</head>
<body>
<h1>List All Address</h1>
<p>Try visting test.php/addresses</p>
<h1>Add a address</h1>
<form id="form1" name="form1" method="post" action="test.php/address">
<p>
<label for="address">address</label>
<input type="text" name="address" id="address" />
</p>
<p>
<label for="city">city</label>
<input type="text" name="city" id="city" />
</p>
<p>
<label for="state">state</label>
<input type="text" name="state" id="state" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>