Skip to content

Instantly share code, notes, and snippets.

@maxired
Created May 3, 2012 22:42
Show Gist options
  • Save maxired/2590101 to your computer and use it in GitHub Desktop.
Save maxired/2590101 to your computer and use it in GitHub Desktop.
A Micro Javascript Page Server à la PHP
var express = require('express');
var util = require('util'),
spawn = require('child_process').spawn;
var app = express.createServer();
app.get('/', function(req, res){
res.send('<ul>'
+ '<li>Download <a href="/helloworld.js">Hello world</a>.</li>'
+ '</ul>');
});
app.get('/:file(*)', function(req, res, next){
var file = req.params.file
, path = __dirname + '/' + file,
child;
child = spawn(process.execPath, [path]);
child.stdout.on('data', function(data){
res.write(data);
}) ;
child.stderr.on('data', function(data){
res.write(data);
}) ;
child.on('exit', function(){
res.end();
});
});
app.listen(3000);
console.log('Express started on port 3000');
console.log("Hello World");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment