summaryrefslogtreecommitdiff
path: root/lib/response.js
diff options
context:
space:
mode:
authorWu Cheng-Han2016-08-19 11:31:23 +0800
committerWu Cheng-Han2016-08-19 11:31:23 +0800
commit87f4d05e8e8f36ac276cd85601cabc1fbb7efc2c (patch)
tree3341cdb5bc1950b1012f6945315cd40d304bfee1 /lib/response.js
parent4d7c3d21206ef61de2323f2138dd99cf5dbf5d31 (diff)
Update to use proper way to render view and fix upload image error should response with code
Diffstat (limited to 'lib/response.js')
-rwxr-xr-xlib/response.js93
1 files changed, 19 insertions, 74 deletions
diff --git a/lib/response.js b/lib/response.js
index b61fe51d..df8abfd3 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;