A pastebin for markdown pages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.4 KiB

11 years ago
var express = require('express');
var page = require('./src/page');
var storage = require('./src/storage');
var md5 = require('md5');
var LRU = require("lru-cache")
var bodyParser = require('body-parser');
10 years ago
11 years ago
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
10 years ago
var CACHE = new LRU(30);
11 years ago
var getTimeStamp = () => {
var timestamp = new Date().getTime();
timestamp = Math.floor(timestamp / 10000000);
return (timestamp).toString(16)
}
11 years ago
app.use(express.static(__dirname + '/resources/public'));
app.get('/new', function (req, res) {
res.send(page.newNotePage(getTimeStamp() + md5(Math.random())));
});
app.post('/note', function (req, res) {
var body = req.body,
session = body.session,
note = body.note,
password = md5(body.password);
var goToNote = note => res.redirect("/" + note.id);
if (session.indexOf(getTimeStamp()) != 0)
10 years ago
return res.status(400).send("Session expired");
10 years ago
var expectedSignature = md5(session + note.replace(/[\n\r]/g, ""));
if (expectedSignature != body.signature)
10 years ago
return res.status(400).send("Signature mismatch");
console.log(body)
if (body.action == "POST")
storage.addNote(note, password).then(goToNote);
else
storage.updateNote(body.id, password, note).then(note => {
CACHE.del(note.id);
goToNote(note);
},
error => res.status(403).send(error.message))
});
11 years ago
app.get("/:year/:month/:day/:title", function (req, res) {
var P = req.params;
storage.getNoteId(P.year + "/" + P.month + "/" + P.day + "/" + P.title)
.then(id => res.redirect("/" + id));
});
app.get(/\/([a-z0-9]+\/edit)/, function (req, res) {
var link = req.params["0"].replace("/edit", "");
storage.getNote(link).then(note =>
res.send(page.editNotePage(getTimeStamp() + md5(Math.random()), note)));
});
10 years ago
app.get(/\/([a-z0-9]+\/export)/, function (req, res) {
var link = req.params["0"].replace("/export", "");
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' });
storage.getNote(link).then(note => res.send(note.text));
});
app.get(/\/([a-z0-9]+)/, function (req, res) {
var link = req.params["0"];
10 years ago
if (CACHE.has(link)) res.send(CACHE.get(link));
else storage.getNote(link).then(note => {
var content = page.buildNote(note);
10 years ago
CACHE.set(link, content);
res.send(content);
});
10 years ago
});
var server = app.listen(3000, function () {
console.log('NoteHub server listening on port %s', server.address().port);
});