Skip to content

Instantly share code, notes, and snippets.

@naeluh
Last active August 29, 2015 14:10
Show Gist options
  • Save naeluh/8e9f957eb0d15597dd75 to your computer and use it in GitHub Desktop.
Save naeluh/8e9f957eb0d15597dd75 to your computer and use it in GitHub Desktop.
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 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.get('/:url_id', function (req, res, next) {
if (data[req.params.url_id]){
console.log('Sent Index');
res.sendFile('/var/www/yourimage/public_html/node_Test/public/index.html');
} else {
console.log('Redirected');
res.redirect('/');
}
});
// I am sending the image here
app.get('/image/:url_id', function (req, res, next) {
if (data[req.params.url_id]){
console.log('Sent Image');
var imgToSend = data[req.params.url_id];
res.send(imgToSend);
} else {
console.log('NotFound');
res.sendStatus(404);
}
});
app.get('/contact', function (req, res) {
res.sendStatus({
your: 'response'
});
});
app.post('/contact', function (req, res) {
var a = req.body.url;
var b = req.body.img;
data[a] = b;
dataChanged = true;
res.sendStatus(204);
});
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