Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created October 5, 2010 19:52
Show Gist options
  • Select an option

  • Save mheadd/612205 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/612205 to your computer and use it in GitHub Desktop.
# Rewrite rules
# On any incoming POST requests, we want to rewrite from http://hostname/resource
# to http://hostname/node/resource
RewriteEngine On
# We only want HTTP POSTs
RewriteCond %{REQUEST_METHOD} "POST"
# If we've already added "/node/" skip the rewrite rule
RewriteCond %{REQUEST_URI} !./node
# Once rewritten, pass thru to the next handler
RewriteRule (.*) /node$1 [PT]
# Depending on your setup, you may need to load required modules in your Virtual Host file.
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
# Access control for proxying
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Set up reverse proxy rules
# Any request URL with "/node/" goes to Node.js script listening to port 9000 on localhost
ProxyRequests Off
ProxyPass /node/ http://127.0.0.1:9000/
ProxyPassReverse /node/ http://127.0.0.1:9000/
/**
* A very simple node web server that demonstrates how to collect
* a 5 digit zip code from a user (via voice, SMS or IM).
*/
var http = require('http');
var tropo = require('../lib/tropo-webapi');
var server = http.createServer(function (request, response) {
// Get the pathname for the current request.
var pathname = require('url').parse(request.url).pathname;
// Add a listener for the data event (incoming data from the HTTP request)
request.addListener('data', function(data){
json = data.toString();
});
// Add a listener for the EOF event on the incoming stream.
request.addListener('end', function() {
try {
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
// The path for the first step in the application flow (ask the caller for input).
if(pathname == '/start') {
var say = new Say("Please enter your 5 digit zip code.");
var choices = new Choices("[5 DIGITS]");
tropo.ask(choices, 3, false, null, "zip", null, true, say, 5, null);
tropo.on("continue", null, '/finish', true);
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(TropoJSON(tropo));
}
// The second step in the application flow - input is submitted via Result JSON delivered from Tropo.
else if(pathname == '/finish') {
// Create a new instance of the Result object and give it the JSON delivered from Tropo.
var result = Result(json);
// Read out the zip code entered by the caller.
// Obviously you could do lots of cool things with this information if you wanted (e.g., look up the weather in the zip code).
// But to keep things simple, we'll just read it back to the caller.
tropo.say("Your zip code is, " + result.interpretation + ". Goodbye.");
tropo.hangup();
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(TropoJSON(tropo));
}
// If an invalid resource is requested, return am HTTP 404.
else {
response.writeHead(404, {'Content-Type': "text/plain"});
response.end("The requested resource does not exist.");
}
}
// If an exception occurs, return an HTTP 500.
catch(e) {
response.writeHead(500, {'Content-Type': "text/plain"});
response.end(e.message + "");
}
})
}).listen(9000, "127.0.0.1"); // Listen to port 9000 on localhost for requests.
@mheadd

mheadd commented Oct 5, 2010

Copy link
Copy Markdown
Author
  • Apache is a nice compliment to a Node.js based web server app because it already does things like access control and logging.
  • No need to replicate this functionality in the Node app.
  • Node app can simply listen to a specified port on localhost.
  • Apache rewrite rules ensure POST requests only get routed to reverse proxy, which sends requests to Node app to be processed.
  • Simple, clean, easy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment