Tiny webserver for testing

Here’s a handy little script. Save the following as webserver.js. This will set up your Raspberry as a bonafide web server that you can use for testing proxy services or your monitoring scripts.

// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("zero1 reporting!\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Once the JavaScript file from Step 4.1.1 is on your raspberry pi, starting it up is as easy as running

node basic_node_webserver.js

from inside a terminal. After doing this, your terminal should look something like this:

$ node webserver.js
Server running at http://127.0.0.1:8000/

Now, you can test connectivity or your new web scraper scrips, without spending a lot of time setting up a sandbox webserver! Obviously, you can edit the listening port or the address. I just like to keep this script on hand so I have to instant services to ping when I’m playing with monitoring scripts and the like.

Author: John

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.