var express = require("express"); var hbs = require('express-hbs'); require('handlebars-form-helpers').register(hbs.handlebars); var bodyParser = require("body-parser"); var app = express(); var winston = require('winston'); winston.add(winston.transports.File, { filename: './logs/notes.kaplon.us.log', maxsize: 5000000 }); // 5MB var fileSystem = require('fs'); app.use(bodyParser.text()); // Use defaults for now, size limit is 100kb. // Use `.hbs` for extensions and find partials in `views/partials`. app.engine('hbs', hbs.express4({ partialsDir: __dirname + '/views/partials' })); app.set('view engine', 'hbs'); app.set('views', __dirname + '/views'); app.use('/lib', express.static('lib')); // these 3 seem hacky, but doing it here instead of view. app.use('/keymap', express.static('keymap')); app.use('/mode', express.static('mode')); var notePath = __dirname + '/note-data/allNotes.txt'; app.get('/', function(req, res){ winston.info("GET /"); // Get curent text from allNotes.txt. // How to pass that text into index.html editor control??? Still need Handlebars??? // i may be grokking it...need to keep handlebars, though. // read contents of allNotes.txt into a variable. // pass that variable as data to handlebars template, {{notetxt}}. fileSystem.readFile(notePath, {encoding: 'utf-8'}, function(err,data){ if (!err){ winston.info('successful file read'); //response.writeHead(200, {'Content-Type': 'text/html'}); //response.write(data); //response.end(); res.render('index', {notetxt: data}, function(err, html) { if(err !== null) { winston.error(err); } else { res.send(html); } }); }else{ winston.error(err); } }); }); app.post('/', function(req, res){ winston.info('POST received'); // Stage and commit changes to allNotes.txt. // quick/dirty poc is to do a fileSystem.write to create a new file w/contents of req.body! var now = Date.now(); fileSystem.writeFile(__dirname + '/note-data/test-' + now + '.txt', req.body, function(err){ if (err) { winston.error(err); } winston.info('new test file written'); }); res.status(204).send('POST received'); }); app.listen(3000, function() { winston.info("Started on PORT 3000"); });