Update to use proper way to render view and fix upload image error should response with code

This commit is contained in:
Wu Cheng-Han 2016-08-19 11:31:23 +08:00
parent 4d7c3d2120
commit 87f4d05e8e
3 changed files with 28 additions and 81 deletions

12
app.js
View file

@ -165,9 +165,11 @@ app.use(function(req, res, next) {
// routes need sessions // routes need sessions
//template files //template files
app.set('views', __dirname + '/public'); app.set('views', __dirname + '/public/views');
//set render engine //set render engine
app.engine('html', ejs.renderFile); app.engine('ejs', ejs.renderFile);
//set view engine
app.set('view engine', 'ejs');
//get index //get index
app.get("/", response.showIndex); app.get("/", response.showIndex);
//get 403 forbidden //get 403 forbidden
@ -185,7 +187,7 @@ app.get("/500", function (req, res) {
//get status //get status
app.get("/status", function (req, res, next) { app.get("/status", function (req, res, next) {
realtime.getStatus(function (data) { realtime.getStatus(function (data) {
res.end(JSON.stringify(data)); res.send(data);
}); });
}); });
//get status //get status
@ -447,11 +449,11 @@ app.post('/uploadimage', function (req, res) {
}) })
.catch(function (err) { .catch(function (err) {
logger.error(err); logger.error(err);
return res.send('upload image error'); return res.status(500).end('upload image error');
}); });
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
return res.send('upload image error'); return res.status(500).end('upload image error');
} }
} }
}); });

View file

@ -1,6 +1,5 @@
//response //response
//external modules //external modules
var ejs = require('ejs');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var markdownpdf = require("markdown-pdf"); var markdownpdf = require("markdown-pdf");
@ -53,40 +52,18 @@ var response = {
}; };
function responseError(res, code, detail, msg) { function responseError(res, code, detail, msg) {
res.writeHead(code, { res.status(code).render(config.errorpath, {
'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({
url: config.serverurl, url: config.serverurl,
title: code + ' ' + detail + ' ' + msg, title: code + ' ' + detail + ' ' + msg,
cache: !config.debug,
filename: template,
code: code, code: code,
detail: detail, detail: detail,
msg: msg, msg: msg,
useCDN: config.usecdn useCDN: config.usecdn
}); });
res.write(content);
res.end();
} }
function showIndex(req, res, next) { function showIndex(req, res, next) {
res.writeHead(200, { res.render(config.indexpath, {
'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({
url: config.serverurl, url: config.serverurl,
useCDN: config.usecdn, useCDN: config.usecdn,
facebook: config.facebook, facebook: config.facebook,
@ -97,8 +74,6 @@ function showIndex(req, res, next) {
google: config.google, google: config.google,
signin: req.isAuthenticated() signin: req.isAuthenticated()
}); });
res.write(content);
res.end();
} }
function responseHackMD(res, note) { function responseHackMD(res, note) {
@ -112,13 +87,11 @@ function responseHackMD(res, note) {
if (!meta) meta = {}; if (!meta) meta = {};
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title); title = models.Note.generateWebTitle(meta.title || title);
var template = config.hackmdpath; res.set({
var options = { 'Cache-Control': 'private', // only cache by client
cache: !config.debug, 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
filename: template });
}; res.render(config.hackmdpath, {
var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options);
var html = compiled({
url: config.serverurl, url: config.serverurl,
title: title, title: title,
useCDN: config.usecdn, useCDN: config.usecdn,
@ -129,14 +102,6 @@ function responseHackMD(res, note) {
dropbox: config.dropbox, dropbox: config.dropbox,
google: config.google 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) { function newNote(req, res, next) {
@ -254,20 +219,10 @@ function showPublishNote(req, res, next) {
} }
function renderPublish(data, res) { function renderPublish(data, res) {
var template = config.prettypath; res.set({
var options = { 'Cache-Control': 'private' // only cache by client
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.end(buf); res.render(config.prettypath, data);
} }
function actionPublish(req, res, note) { function actionPublish(req, res, note) {
@ -283,17 +238,17 @@ function actionDownload(req, res, note) {
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
var filename = title; var filename = title;
filename = encodeURIComponent(filename); filename = encodeURIComponent(filename);
res.writeHead(200, { res.set({
'Access-Control-Allow-Origin': '*', //allow CORS as API 'Access-Control-Allow-Origin': '*', //allow CORS as API
'Access-Control-Allow-Headers': 'Range', 'Access-Control-Allow-Headers': 'Range',
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range', 'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Content-Type': 'text/markdown; charset=UTF-8', 'Content-Type': 'text/markdown; charset=UTF-8',
'Cache-Control': 'private', 'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + filename + '.md', 'Content-disposition': 'attachment; filename=' + filename + '.md',
'Content-Length': body.length,
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
}); });
res.end(body); res.send(body);
}
function actionInfo(req, res, note) { function actionInfo(req, res, note) {
var body = LZString.decompressFromBase64(note.content); var body = LZString.decompressFromBase64(note.content);
@ -377,7 +332,7 @@ function actionRevision(req, res, note) {
if (!content) { if (!content) {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
res.end(JSON.stringify(content)); res.send(content);
}); });
} else { } else {
return response.errorNotFound(res); return response.errorNotFound(res);
@ -391,7 +346,7 @@ function actionRevision(req, res, note) {
var out = { var out = {
revision: data revision: data
}; };
res.end(JSON.stringify(out)); res.send(out);
}); });
} }
} }
@ -635,20 +590,10 @@ function showPublishSlide(req, res, next) {
} }
function renderPublishSlide(data, res) { function renderPublishSlide(data, res) {
var template = config.slidepath; res.set({
var options = { 'Cache-Control': 'private' // only cache by client
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.end(buf); res.render(config.slidepath, data);
} }
module.exports = response; module.exports = response;

View file

@ -1512,7 +1512,7 @@ var revisionTime = null;
ui.modal.revision.on('show.bs.modal', function (e) { ui.modal.revision.on('show.bs.modal', function (e) {
$.get(noteurl + '/revision') $.get(noteurl + '/revision')
.done(function(data) { .done(function(data) {
parseRevisions(JSON.parse(data).revision); parseRevisions(data.revision);
initRevisionViewer(); initRevisionViewer();
}) })
.fail(function(err) { .fail(function(err) {
@ -1564,7 +1564,7 @@ function selectRevision(time) {
if (time == revisionTime) return; if (time == revisionTime) return;
$.get(noteurl + '/revision/' + time) $.get(noteurl + '/revision/' + time)
.done(function(data) { .done(function(data) {
revision = JSON.parse(data); revision = data;
revisionTime = time; revisionTime = time;
var lastScrollInfo = revisionViewer.getScrollInfo(); var lastScrollInfo = revisionViewer.getScrollInfo();
revisionList.children().removeClass('active'); revisionList.children().removeClass('active');