Hemanth's Scribes

node

HTTP Status Code as a Service

Author Photo

Hemanth HM

Thumbnail

A simple HTTP status service built with Node.js.

var http = require('http');
var url = require("url");

http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  
  var url_parts = url.parse(req.url, true);
  var status = url_parts.pathname.replace(/^\/([^\/]*).*$/, '$1');
  
  res.end(
    http.STATUS_CODES[status] || 
    "Undefined status: " + status || 
    "Try /404"
  );
}).listen(process.env.PORT || 5000);

Try it: Request /418 and get I'm a teapot!

#node#javascript
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.