Skip to content

Instantly share code, notes, and snippets.

@polotek
Created May 9, 2010 17:13
Show Gist options
  • Save polotek/395282 to your computer and use it in GitHub Desktop.
Save polotek/395282 to your computer and use it in GitHub Desktop.
process.addListener('uncaughtException', function(err) {
throw err;
});
var sys = require('sys')
, fs = require('fs')
, http = require('http');
http.createServer(function (req, res) {
sys.debug(req.url);
req.setEncoding('utf8');
if(/ajax/.test(req.url)) {
var t = JSON.stringify({id:"test"});
res.writeHead(200, {
'Content-Length': t.length
, 'Content-Type': 'text/json'
});
res.write(t);
res.end();
} else {
fs.readFile('test.html', 'utf8', function(err, data) {
if(err) {
res.writeHead(500, {});
res.end();
throw err;
}
res.writeHead(200, {
'Content-Length': data.length
, 'Content-Type': 'text/html'
});
res.write(data);
res.end();
});
}
}).listen(8000);
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page title</title>
</head>
<body>
<h1>Test</h1>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<script type="text/javascript">
$(function() {
$.getJSON('http://localhost:8000/ajax?' + (new Date()).getTime(), function(data, textStatus) { alert(data, textStatus); } );
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment