Skip to content

Instantly share code, notes, and snippets.

@tomalex0
Last active December 21, 2015 23:59
Show Gist options
  • Select an option

  • Save tomalex0/6386470 to your computer and use it in GitHub Desktop.

Select an option

Save tomalex0/6386470 to your computer and use it in GitHub Desktop.
Basic Node server setup config which allows to make Ajax POST call to local json files.
var express = require('express');
var app = express();
var path = require('path');
var fs = require('fs');
app.use(express.compress());
app.use(express.static(__dirname));
function readJsonFileSync(filepath, encoding){
if (typeof (encoding) == 'undefined'){
encoding = 'utf8';
}
var file = fs.readFileSync(filepath, encoding);
return JSON.parse(file);
}
app.post('*', function(request,respond){
var filepath = __dirname + ''+request.url;
var url = filepath.split("?");
var json = readJsonFileSync(path.resolve(url[0]));
respond.json(json);
});
var port = 212;
app.listen(process.env.PORT || port);
console.log("localhost:"+port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment