4 ms·
18 lines of node.js to accomplish something similar - I use something similar as a function in my node.js scripts to be able to send a generic error template ba
by elliottcarlson 16y ago
18 lines of node.js to accomplish something similar - I use something similar as a function in my node.js scripts to be able to send a generic error template based on the code that I pass back to the function.
var http = require('http');
http.createServer(function (request, response)
{
var statusCode = request.url.substr(1, 3);
response.writeHead(statusCode, { 'connection' : 'close', 'content-type' : 'text/html' });
response.write('<html>');
response.write('<head><title>' + statusCode + ' ' + http.STATUS_CODES[statusCode] + '</title></head>');
response.write('<body bgcolor="white">');
response.write('<center><h1>' + statusCode + ' ' + http.STATUS_CODES[statusCode] + '</h1></center>');
response.write(' '.repeat(512));
response.write('</body>');
response.end('</html>');
}).listen(8124, "0.0.0.0");
String.prototype.repeat = function(num)
{
return new Array(num++).join(this);
}
- jsdalton 16y agoHere's a little something similar I whipped up using Flask (Python): import time from flask import Flask, make_response, request app = Flask(__name__) @app.route('/<int:status_code>') def return_status(status_code): response = make_response() response.status_code = status_code response.data = response.status sleep = request.args.get('sleep', None) if sleep is not None: time.sleep(int(sleep)) return response if __name__ == '__main__': app.run(debug=True) With a handy little feature, a "sleep" query string param that'll delay the response for the given number of seconds, which I've used to test how a client handles latency etc. [BTW, looks like you're the Elliot Carlson I used to work with way back when. Hope life is treating you well these days! :)]
- elliottcarlson 16y agoHah, indeed! Email me some time - x at sublim dot nl
- tatham 16y agoThis will get you 80% of the way there, but some status codes require certain response headers too. We return these too.
- johns 16y agosignatures are discouraged here: http://ycombinator.com/newsguidelines.html http://ycombinator.com/newsguidelines.html