Refactor to get rid of handlebars templates, no longer needed with websocket working.
This commit is contained in:
		
							parent
							
								
									97bf71306b
								
							
						
					
					
						commit
						fb4637c2a1
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -3,6 +3,5 @@ | ||||
| /CodeMirror/* | ||||
| /codemirror/* | ||||
| /note-data/* | ||||
| index.html | ||||
| /logs/* | ||||
| /node_modules/* | ||||
|  | ||||
							
								
								
									
										61
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								server.js
									
									
									
									
									
								
							| @ -1,6 +1,4 @@ | ||||
| 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'); | ||||
| @ -42,12 +40,6 @@ passport.deserializeUser(function(id, cb) { | ||||
|   }); | ||||
| }); | ||||
| 
 | ||||
| 
 | ||||
| // 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'); | ||||
| var favicon = require('serve-favicon'); | ||||
| app.use(favicon(__dirname + '/assets/favicon.ico'));  // Put this before setting static dir.
 | ||||
| @ -56,7 +48,7 @@ app.use(express.static('assets')); | ||||
| app.use(require('cookie-parser')()); | ||||
| app.use(bodyParser.text());   // Use defaults for now, size limit is 100kb.
 | ||||
| app.use(bodyParser.urlencoded({ extended: true }));   // Also need url encoding to handle login form.
 | ||||
| app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false })); | ||||
| app.use(require('express-session')({ secret: 'here kitty kitty', resave: false, saveUninitialized: false })); | ||||
| var notePath = __dirname + '/note-data/allNotes.txt'; | ||||
| 
 | ||||
| // Initialize Passport and restore authentication state, if any, from the session.
 | ||||
| @ -76,50 +68,13 @@ var isAuthenticated = function (req, res, next) { | ||||
| 
 | ||||
| app.get('/', isAuthenticated, function(req, res){ | ||||
|     winston.info("GET /"); | ||||
|     // Get curent text from allNotes.txt and pass that data to handlebars template.
 | ||||
|     fileSystem.readFile(notePath, {encoding: 'utf-8'}, function(err,data){ | ||||
|         if (!err){ | ||||
|             winston.info('successful file read'); | ||||
|             res.render('index', {notetxt: data}, function(err, html) { | ||||
|                 if(err !== null) { | ||||
|                     winston.error(err); | ||||
|                 } else { | ||||
|                     res.send(html); | ||||
|                 } | ||||
|             }); | ||||
|         }else{ | ||||
|             winston.error(err); | ||||
|         } | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| app.post('/', isAuthenticated, function(req, res){ | ||||
|     winston.info('POST received'); | ||||
|     var now = Date.now(); | ||||
|     // Overwrite allNotes.txt with new contents from client.
 | ||||
|     fileSystem.readFile(notePath, 'utf-8', function(err, data){ | ||||
|         if (err) {  | ||||
|             winston.error(err);  | ||||
|         } else { | ||||
|             fileSystem.writeFile(notePath, req.body, 'utf-8', function(err) { | ||||
|                 if (err) { winston.error(err); } | ||||
|                 winston.info('new contents from client written to allNotes.txt'); | ||||
|                 var exec = require('child_process').exec; | ||||
|                 var cmd = 'cd note-data && git commit -am "Notes modified via POST request, ' + now + '"'; | ||||
|                 winston.info(cmd); | ||||
|                 exec(cmd, function(error, stdout, stderr) { | ||||
|                     if (error) { winston.error(error); } | ||||
|                     winston.info(stdout); | ||||
|                 }); | ||||
|             }); | ||||
|         } | ||||
|     }); | ||||
|     res.status(204).send('POST received'); | ||||
|     // Respond with static file, contents will be loaded via websocket.
 | ||||
|     res.sendFile(__dirname +'/views/index.html'); | ||||
| }); | ||||
| 
 | ||||
| app.get('/login', function(req, res){ | ||||
|     winston.info('GET /login'); | ||||
|     res.render('login'); | ||||
|     res.sendFile(__dirname +'/views/login.html'); | ||||
| }); | ||||
|    | ||||
| app.post('/login',  | ||||
| @ -141,10 +96,10 @@ io.on('connection', function(socket){ | ||||
|     fileSystem.readFile(notePath, {encoding: 'utf-8'}, function(err,data){ | ||||
|         if (!err){ | ||||
|             winston.info('successful file read'); | ||||
|             io.emit('sending allNotes',data); | ||||
|             io.emit('download allNotes',data); | ||||
|         } else { winston.error(err); } | ||||
|     }); | ||||
|     socket.on('sending allNotes', function(msg){ | ||||
|     socket.on('upload allNotes', function(msg){ | ||||
|         winston.info('WS data received'); | ||||
|         var now = Date.now(); | ||||
|         // Overwrite allNotes.txt with new contents from client.
 | ||||
| @ -171,10 +126,6 @@ io.on('connection', function(socket){ | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| //setInterval(function() {
 | ||||
|     //io.emit('server event', Date.now());
 | ||||
| //},30000);
 | ||||
| 
 | ||||
| http.listen(3000, function() { | ||||
|     winston.info("Started on PORT 3000"); | ||||
| }); | ||||
|  | ||||
| @ -70,16 +70,11 @@ | ||||
|     //CodeMirror.Vim.map(' c', '<Esc>o- [ ] ', 'normal') | ||||
|     //CodeMirror.Vim.map(' x', '<ESC>;s/\[\s\]/[x]/g<CR>;noh<CR>', 'normal') | ||||
|     CodeMirror.commands.save = function(){ | ||||
|         //alert("Saving"); | ||||
|         var socket = io(); | ||||
|         socket.emit('cm-save', 'codemirror save event'); | ||||
|     } | ||||
| 
 | ||||
|     var socket = io(); | ||||
|     //socket.on('server event', function(msg){ | ||||
|         //console.log(msg); | ||||
|     //}); | ||||
|     socket.on('sending allNotes', function(msg){ | ||||
|     socket.on('download allNotes', function(msg){ | ||||
|         //console.log(msg); | ||||
|         editor.getDoc().setValue(msg); | ||||
|     }); | ||||
| @ -89,21 +84,11 @@ | ||||
|     var typingTimer; | ||||
|     editor.on("changes", function() { | ||||
|         if (!userKeypress) { return; };  // Do nothing if no keys hit yet, avoids infinite loop since 'changes' event fires after initial websocket content loading. | ||||
|         userKeypress = false;   // Set this back to false, still getting infinite loop. | ||||
|         clearTimeout(typingTimer); | ||||
|         typingTimer = setTimeout( | ||||
|         function() { | ||||
|             //console.log(editor.getValue()); | ||||
|            //var request = new XMLHttpRequest(); | ||||
|             //request.onreadystatechange = function () { | ||||
|                 //var DONE = this.DONE || 4; | ||||
|                 //if (this.readyState === DONE){ | ||||
|                     //console.log('ajax is done.'); | ||||
|                 //} | ||||
|             //}; | ||||
|             //request.open('POST', '/', true); | ||||
|             //request.send(editor.getValue());  | ||||
|             var socket = io(); | ||||
|             socket.emit('sending allNotes', editor.getValue()); | ||||
|             socket.emit('upload allNotes', editor.getValue()); | ||||
|         }, | ||||
|         2000 | ||||
|       ); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user