Last active
August 29, 2015 14:10
-
-
Save naeluh/2e5e0c7fab52e5eca863 to your computer and use it in GitHub Desktop.
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 fs = require('fs'); | |
var bodyParser = require('body-parser'); | |
var express = require('express'); | |
var dataChanged = false; | |
var foundIt = false; | |
var dataFile = './db/page.json'; | |
var image; | |
var data = require(dataFile); | |
var app = express(); | |
app.use(bodyParser.json({ | |
limit: '50mb' | |
})); | |
app.use(bodyParser.urlencoded({ | |
limit: '50mb', | |
extended: true | |
})); | |
app.use(express.static(__dirname + '/public')); | |
app.param('url_id', function (req, res, next, url_id) { | |
if (data[url_id]){ | |
console.log('Found it!'); | |
//console.log(data[url_id]); | |
next(); | |
} else { | |
res.redirect('/'); | |
} | |
}); | |
app.get('/:url_id', function (req, res, next) { | |
console.log('Here'); | |
//console.log(data[url_id]); | |
}); | |
app.get('/contact', function (req, res) { | |
res.send({ | |
your: 'response' | |
}); | |
}); | |
app.post('/contact', function (req, res) { | |
var a = req.body.url; | |
var b = req.body.img; | |
console.log(a); | |
//var page = {}; | |
data[a] = b; | |
//console.log(page.a); | |
//data.push(page); | |
dataChanged = true; | |
res.send(204); | |
}); | |
// every 10 seconds, save the data to disk if it has changed | |
setInterval(function () { | |
if (!dataChanged) return; | |
var str = JSON.stringify(data, null, 4); | |
dataChanged = false; | |
fs.writeFile(dataFile, str, function (err) { | |
console.log(err ? err : 'JSON saved.'); | |
}); | |
}, 10000); | |
app.listen(5446, function () { | |
console.log('listening') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment