From 87f4d05e8e8f36ac276cd85601cabc1fbb7efc2c Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Fri, 19 Aug 2016 11:31:23 +0800 Subject: [PATCH] Update to use proper way to render view and fix upload image error should response with code --- app.js | 12 +++--- lib/response.js | 93 ++++++++++------------------------------------ public/js/index.js | 4 +- 3 files changed, 28 insertions(+), 81 deletions(-) diff --git a/app.js b/app.js index 54a6baa..c098576 100644 --- a/app.js +++ b/app.js @@ -165,9 +165,11 @@ app.use(function(req, res, next) { // routes need sessions //template files -app.set('views', __dirname + '/public'); +app.set('views', __dirname + '/public/views'); //set render engine -app.engine('html', ejs.renderFile); +app.engine('ejs', ejs.renderFile); +//set view engine +app.set('view engine', 'ejs'); //get index app.get("/", response.showIndex); //get 403 forbidden @@ -185,7 +187,7 @@ app.get("/500", function (req, res) { //get status app.get("/status", function (req, res, next) { realtime.getStatus(function (data) { - res.end(JSON.stringify(data)); + res.send(data); }); }); //get status @@ -447,11 +449,11 @@ app.post('/uploadimage', function (req, res) { }) .catch(function (err) { logger.error(err); - return res.send('upload image error'); + return res.status(500).end('upload image error'); }); } catch (err) { logger.error(err); - return res.send('upload image error'); + return res.status(500).end('upload image error'); } } }); diff --git a/lib/response.js b/lib/response.js index b61fe51..df8abfd 100755 --- a/lib/response.js +++ b/lib/response.js @@ -1,6 +1,5 @@ //response //external modules -var ejs = require('ejs'); var fs = require('fs'); var path = require('path'); var markdownpdf = require("markdown-pdf"); @@ -53,40 +52,18 @@ var response = { }; function responseError(res, code, detail, msg) { - res.writeHead(code, { - 'Content-Type': 'text/html' - }); - var template = config.errorpath; - var options = { - cache: !config.debug, - filename: template - }; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); - var content = compiled({ + res.status(code).render(config.errorpath, { url: config.serverurl, title: code + ' ' + detail + ' ' + msg, - cache: !config.debug, - filename: template, code: code, detail: detail, msg: msg, - useCDN: config.usecdn + useCDN: config.usecdn }); - res.write(content); - res.end(); } function showIndex(req, res, next) { - res.writeHead(200, { - 'Content-Type': 'text/html' - }); - var template = config.indexpath; - var options = { - cache: !config.debug, - filename: template - }; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); - var content = compiled({ + res.render(config.indexpath, { url: config.serverurl, useCDN: config.usecdn, facebook: config.facebook, @@ -97,8 +74,6 @@ function showIndex(req, res, next) { google: config.google, signin: req.isAuthenticated() }); - res.write(content); - res.end(); } function responseHackMD(res, note) { @@ -112,13 +87,11 @@ function responseHackMD(res, note) { if (!meta) meta = {}; var title = models.Note.decodeTitle(note.title); title = models.Note.generateWebTitle(meta.title || title); - var template = config.hackmdpath; - var options = { - cache: !config.debug, - filename: template - }; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); - var html = compiled({ + res.set({ + 'Cache-Control': 'private', // only cache by client + 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling + }); + res.render(config.hackmdpath, { url: config.serverurl, title: title, useCDN: config.usecdn, @@ -129,14 +102,6 @@ function responseHackMD(res, note) { dropbox: config.dropbox, google: config.google }); - var buf = html; - res.writeHead(200, { - 'Content-Type': 'text/html; charset=UTF-8', - 'Cache-Control': 'private', - 'Content-Length': buf.length, - 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling - }); - res.end(buf); } function newNote(req, res, next) { @@ -254,20 +219,10 @@ function showPublishNote(req, res, next) { } function renderPublish(data, res) { - var template = config.prettypath; - var options = { - cache: !config.debug, - filename: template - }; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); - var html = compiled(data); - var buf = html; - res.writeHead(200, { - 'Content-Type': 'text/html; charset=UTF-8', - 'Cache-Control': 'private', - 'Content-Length': buf.length + res.set({ + 'Cache-Control': 'private' // only cache by client }); - res.end(buf); + res.render(config.prettypath, data); } function actionPublish(req, res, note) { @@ -283,17 +238,17 @@ function actionDownload(req, res, note) { var title = models.Note.decodeTitle(note.title); var filename = title; filename = encodeURIComponent(filename); - res.writeHead(200, { + res.set({ 'Access-Control-Allow-Origin': '*', //allow CORS as API 'Access-Control-Allow-Headers': 'Range', 'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range', 'Content-Type': 'text/markdown; charset=UTF-8', 'Cache-Control': 'private', 'Content-disposition': 'attachment; filename=' + filename + '.md', - 'Content-Length': body.length, 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling }); - res.end(body); + res.send(body); +} function actionInfo(req, res, note) { var body = LZString.decompressFromBase64(note.content); @@ -377,7 +332,7 @@ function actionRevision(req, res, note) { if (!content) { return response.errorNotFound(res); } - res.end(JSON.stringify(content)); + res.send(content); }); } else { return response.errorNotFound(res); @@ -391,7 +346,7 @@ function actionRevision(req, res, note) { var out = { revision: data }; - res.end(JSON.stringify(out)); + res.send(out); }); } } @@ -635,20 +590,10 @@ function showPublishSlide(req, res, next) { } function renderPublishSlide(data, res) { - var template = config.slidepath; - var options = { - cache: !config.debug, - filename: template - }; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); - var html = compiled(data); - var buf = html; - res.writeHead(200, { - 'Content-Type': 'text/html; charset=UTF-8', - 'Cache-Control': 'private', - 'Content-Length': buf.length + res.set({ + 'Cache-Control': 'private' // only cache by client }); - res.end(buf); + res.render(config.slidepath, data); } module.exports = response; diff --git a/public/js/index.js b/public/js/index.js index 43b7525..ac17152 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -1512,7 +1512,7 @@ var revisionTime = null; ui.modal.revision.on('show.bs.modal', function (e) { $.get(noteurl + '/revision') .done(function(data) { - parseRevisions(JSON.parse(data).revision); + parseRevisions(data.revision); initRevisionViewer(); }) .fail(function(err) { @@ -1564,7 +1564,7 @@ function selectRevision(time) { if (time == revisionTime) return; $.get(noteurl + '/revision/' + time) .done(function(data) { - revision = JSON.parse(data); + revision = data; revisionTime = time; var lastScrollInfo = revisionViewer.getScrollInfo(); revisionList.children().removeClass('active');