Last active
August 29, 2015 14:10
-
-
Save naeluh/24887ad4c388bdeb41fd to your computer and use it in GitHub Desktop.
server.js that has a pretty simple url structure thru node.js express
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 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('/'); | |
} | |
}); | |
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.'); | |
}); | |
//res.redirect('/'+a); | |
}, 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