jeff-hager-dev
12/19/2014 - 3:12 PM

Super simple NodeJS/Express Server for testing or prototyping an API call. Also this is set up to avoid CORS issues with testing, so if you

Super simple NodeJS/Express Server for testing or prototyping an API call. Also this is set up to avoid CORS issues with testing, so if you want to set it up to act as a mock CDN go for it OR an external API mock do it.

{
  "name": "simpleExpressServer",
  "version": "1.0.0",
  "description": "A simple express server used for testing calls or prototyping a api call",
  "main": "app.js",
  "dependencies": {
    "express": "^4.10.6"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "nodejs",
    "express",
    "server",
    "api"
  ],
  "author": "J. Nolan Hager (JNHager)"
}
var express = require('express');
var fs = require('fs');
var path = require('path');

var port = 3005;
var staticDir = "staticFiles"
var app = express();

app.use(function (req, res, next) {
  //NOTE (JNHager): NEVER DO THIS IN A PRODUCTION SYSTEM. STRICTLY FOR TESTING LOCALLY!!!! 
  //NOTE (JNHager): SERIOUSLY DON'T, GOD WILL KILL KITTENS IF YOU DO THIS IN A PRODUCTION SYSTEM
  res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  next();
});


app.get("/", function(req, res) {
  res.status(200)
    .send({message: "Dude, this is a server"});
});

app.post("/", function(req, res) {
  res.status(200)
    .send({message: "Dude, this is a server"});
});

/* Servers static files 
=============================================================================*/
app.use('/staticfile', express.static(staticDir));

/* Starting the Server
=============================================================================*/
app.listen(port, function() {
  console.log("Simple Server Listening on " + port);
});