Skip to content

Instantly share code, notes, and snippets.

@jonalmeida
Last active December 20, 2016 01:35
Show Gist options
  • Save jonalmeida/782edd1091fe85c48074744a73eed9b3 to your computer and use it in GitHub Desktop.
Save jonalmeida/782edd1091fe85c48074744a73eed9b3 to your computer and use it in GitHub Desktop.
Simple test web server

Simple test web server

Requirements

  • NodeJS
    • Preferably the latest or (if you're on a Mac) with brew install nodejs.

Instructions

  1. Put the package.json and app.js files in a folder
  2. Run npm install
  3. Edit the app.get and app.post examples to fit your needs.

Disclaimer

I probably got this snippet from somewhere on the internet but unfortunately I didn't save the original source location to reference back to it.

var express = require('express');
var app = express();
/* serves main page */
app.get("/", function(req, res) {
res.send("help me, this server is so lame.");
});
app.get("/setup/offer", function(req, res) {
console.log("Getting request...\n===");
//console.log(req);
//console.log("===");
res.send("help me, this server is so lame.");
});
app.post("/user/add", function(req, res) {
/* some server side logic */
console.log(req);
res.send("OK");
});
/* serves all the static files */
app.get(/^(.+)$/, function(req, res){
console.log('static file request : ' + req.params);
res.sendfile( __dirname + req.params[0]);
});
var port = process.env.PORT || 8008;
app.listen(port, function() {
console.log("Listening on " + port);
});
{
"name": "simple-http-server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"express": "^4.14.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment