const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); app.set('view engine', 'ejs'); app.use(express.static('static')); app.get('/', (req, res) => { res.render('index.ejs'); }); app.post('/write', (req, res) => { let filename = require('crypto').randomBytes(16).toString('hex'); let datainput = req.body; let fs = require('fs'); if(datainput.text == undefined || datainput.text == "") return res.send('Error: Text is invalid').status(403); if(fs.existsSync(__dirname+'/bin/'+filename+'.json')) return res.send('Internal error - randomly generated ID already exists? Try again.').status(500); let data = { name: datainput.name == undefined ? '' : datainput.name, text: datainput.text } fs.writeFile(__dirname+'/bin/'+filename+'.json', JSON.stringify(data), (err) => { if (err) { console.log(err); res.status(500); } else { res.send(filename).status(200); } }); }); app.get('/new', (req, res) => { res.render('new.ejs'); }); app.get('/paste/:id', (req, res) => { let fs = require('fs'); let filename = req.params.id; if(!fs.existsSync(__dirname+'/bin/'+filename+'.json')) return res.send('Error: Paste not found').status(404); let data = JSON.parse(fs.readFileSync(__dirname+'/bin/'+filename+'.json', 'utf8')); res.render('paste.ejs', { name: data.name, text: data.text, id: filename }); }); app.listen(30180);