Skip to content

Instantly share code, notes, and snippets.

@jeff-hager-dev
Created December 19, 2014 15:12
Show Gist options
  • Save jeff-hager-dev/71a8b39f0e3b88babeee to your computer and use it in GitHub Desktop.
Save jeff-hager-dev/71a8b39f0e3b88babeee to your computer and use it in GitHub Desktop.
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.
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);
});
{
"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)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment