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

View file

@ -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;