Last active
December 21, 2015 23:59
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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