mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 08:20:36 +01:00
linting webserver.js
This commit is contained in:
@@ -67,8 +67,11 @@ Upgrade.upgrade = function() {
|
|||||||
var multi = RDB.multi();
|
var multi = RDB.multi();
|
||||||
|
|
||||||
keys = keys.filter(function(key) {
|
keys = keys.filter(function(key) {
|
||||||
if (key === 'notifications:next_nid') return false;
|
if (key === 'notifications:next_nid') {
|
||||||
else return true;
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}).map(function(key) {
|
}).map(function(key) {
|
||||||
return key.slice(14);
|
return key.slice(14);
|
||||||
});
|
});
|
||||||
|
|||||||
155
src/webserver.js
155
src/webserver.js
@@ -28,15 +28,19 @@ var express = require('express'),
|
|||||||
logger = require('./logger.js');
|
logger = require('./logger.js');
|
||||||
|
|
||||||
(function (app) {
|
(function (app) {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
var templates = null,
|
var templates = null,
|
||||||
clientScripts;
|
clientScripts;
|
||||||
|
|
||||||
// Minify client-side libraries
|
// Minify client-side libraries
|
||||||
meta.js.get(function (err, scripts) {
|
meta.js.get(function (err, scripts) {
|
||||||
clientScripts = scripts.map(function (script) {
|
clientScripts = scripts.map(function (script) {
|
||||||
return script = {
|
script = {
|
||||||
script: script
|
script: script
|
||||||
}
|
};
|
||||||
|
|
||||||
|
return script;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -66,13 +70,13 @@ var express = require('express'),
|
|||||||
content: meta.config.title || 'NodeBB'
|
content: meta.config.title || 'NodeBB'
|
||||||
}, {
|
}, {
|
||||||
property: 'keywords',
|
property: 'keywords',
|
||||||
content: meta.config['keywords'] || ''
|
content: meta.config.keywords || ''
|
||||||
}],
|
}],
|
||||||
metaString = utils.buildMetaTags(defaultMetaTags.concat(options.metaTags || [])),
|
metaString = utils.buildMetaTags(defaultMetaTags.concat(options.metaTags || [])),
|
||||||
linkTags = utils.buildLinkTags(options.linkTags || []),
|
linkTags = utils.buildLinkTags(options.linkTags || []),
|
||||||
templateValues = {
|
templateValues = {
|
||||||
cssSrc: meta.config['theme:src'] || nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
|
cssSrc: meta.config['theme:src'] || nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
|
||||||
pluginCSS: plugins.cssFiles.map(function(file) { return { path: file } }),
|
pluginCSS: plugins.cssFiles.map(function(file) { return { path: file }; }),
|
||||||
title: meta.config.title || '',
|
title: meta.config.title || '',
|
||||||
description: meta.config.description || '',
|
description: meta.config.description || '',
|
||||||
'brand:logo': meta.config['brand:logo'] || '',
|
'brand:logo': meta.config['brand:logo'] || '',
|
||||||
@@ -88,8 +92,9 @@ var express = require('express'),
|
|||||||
|
|
||||||
var uid = '0';
|
var uid = '0';
|
||||||
|
|
||||||
if(options.req.user && options.req.user.uid)
|
if(options.req.user && options.req.user.uid) {
|
||||||
uid = options.req.user.uid;
|
uid = options.req.user.uid;
|
||||||
|
}
|
||||||
|
|
||||||
user.isAdministrator(uid, function(isAdmin) {
|
user.isAdministrator(uid, function(isAdmin) {
|
||||||
templateValues.adminDisplay = isAdmin ? 'show' : 'hide';
|
templateValues.adminDisplay = isAdmin ? 'show' : 'hide';
|
||||||
@@ -97,7 +102,7 @@ var express = require('express'),
|
|||||||
translator.translate(templates.header.parse(templateValues), function(template) {
|
translator.translate(templates.header.parse(templateValues), function(template) {
|
||||||
callback(null, template);
|
callback(null, template);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -249,11 +254,17 @@ var express = require('express'),
|
|||||||
res.json(200, {});
|
res.json(200, {});
|
||||||
} else if (req.accepts('html')) {
|
} else if (req.accepts('html')) {
|
||||||
// respond with html page
|
// respond with html page
|
||||||
if (process.env.NODE_ENV === 'development') winston.warn('Route requested but not found: ' + req.url);
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.warn('Route requested but not found: ' + req.url);
|
||||||
|
}
|
||||||
|
|
||||||
res.redirect(nconf.get('relative_path') + '/404');
|
res.redirect(nconf.get('relative_path') + '/404');
|
||||||
} else if (req.accepts('json')) {
|
} else if (req.accepts('json')) {
|
||||||
// respond with json
|
// respond with json
|
||||||
if (process.env.NODE_ENV === 'development') winston.warn('Route requested but not found: ' + req.url);
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.warn('Route requested but not found: ' + req.url);
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
error: 'Not found'
|
error: 'Not found'
|
||||||
});
|
});
|
||||||
@@ -284,7 +295,9 @@ var express = require('express'),
|
|||||||
winston.error('Errors were encountered while attempting to initialise NodeBB.');
|
winston.error('Errors were encountered while attempting to initialise NodeBB.');
|
||||||
process.exit();
|
process.exit();
|
||||||
} else {
|
} else {
|
||||||
if (process.env.NODE_ENV === 'development') winston.info('Middlewares loaded.');
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('Middlewares loaded.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -293,16 +306,16 @@ var express = require('express'),
|
|||||||
templates = global.templates;
|
templates = global.templates;
|
||||||
|
|
||||||
// translate all static templates served by webserver here. ex. footer, logout
|
// translate all static templates served by webserver here. ex. footer, logout
|
||||||
translator.translate(templates['footer'].toString(), function(parsedTemplate) {
|
translator.translate(templates.footer.toString(), function(parsedTemplate) {
|
||||||
templates['footer'] = parsedTemplate;
|
templates.footer = parsedTemplate;
|
||||||
});
|
});
|
||||||
translator.translate(templates['logout'].toString(), function(parsedTemplate) {
|
translator.translate(templates.logout.toString(), function(parsedTemplate) {
|
||||||
templates['logout'] = parsedTemplate;
|
templates.logout = parsedTemplate;
|
||||||
});
|
});
|
||||||
|
|
||||||
winston.info('NodeBB Ready');
|
winston.info('NodeBB Ready');
|
||||||
server.listen(nconf.get('PORT') || nconf.get('port'), nconf.get('bind_address'));
|
server.listen(nconf.get('PORT') || nconf.get('port'), nconf.get('bind_address'));
|
||||||
}
|
};
|
||||||
|
|
||||||
app.create_route = function (url, tpl) { // to remove
|
app.create_route = function (url, tpl) { // to remove
|
||||||
return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>';
|
return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>';
|
||||||
@@ -336,7 +349,7 @@ var express = require('express'),
|
|||||||
req: req,
|
req: req,
|
||||||
res: res
|
res: res
|
||||||
}, function (err, header) {
|
}, function (err, header) {
|
||||||
res.send((isNaN(parseInt(route, 10)) ? 200 : parseInt(route, 10)), header + app.create_route(route) + templates['footer']);
|
res.send((isNaN(parseInt(route, 10)) ? 200 : parseInt(route, 10)), header + app.create_route(route) + templates.footer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}(routes[i]));
|
}(routes[i]));
|
||||||
@@ -368,8 +381,11 @@ var express = require('express'),
|
|||||||
"categories": function (next) {
|
"categories": function (next) {
|
||||||
categories.getAllCategories(function (returnData) {
|
categories.getAllCategories(function (returnData) {
|
||||||
returnData.categories = returnData.categories.filter(function (category) {
|
returnData.categories = returnData.categories.filter(function (category) {
|
||||||
if (category.disabled !== '1') return true;
|
if (category.disabled !== '1') {
|
||||||
else return false;
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
next(null, returnData);
|
next(null, returnData);
|
||||||
@@ -380,12 +396,11 @@ var express = require('express'),
|
|||||||
data.header +
|
data.header +
|
||||||
'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/home'].parse(data.categories) + '\n\t</noscript>' +
|
'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/home'].parse(data.categories) + '\n\t</noscript>' +
|
||||||
app.create_route('') +
|
app.create_route('') +
|
||||||
templates['footer']
|
templates.footer
|
||||||
);
|
);
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get('/topic/:topic_id/:slug?', function (req, res) {
|
app.get('/topic/:topic_id/:slug?', function (req, res) {
|
||||||
var tid = req.params.topic_id;
|
var tid = req.params.topic_id;
|
||||||
|
|
||||||
@@ -394,18 +409,26 @@ var express = require('express'),
|
|||||||
var rssPath = path.join(__dirname, '../', 'feeds/topics', tid + '.rss'),
|
var rssPath = path.join(__dirname, '../', 'feeds/topics', tid + '.rss'),
|
||||||
loadFeed = function () {
|
loadFeed = function () {
|
||||||
fs.readFile(rssPath, function (err, data) {
|
fs.readFile(rssPath, function (err, data) {
|
||||||
if (err) res.type('text').send(404, "Unable to locate an rss feed at this location.");
|
if (err) {
|
||||||
else res.type('xml').set('Content-Length', data.length).send(data);
|
res.type('text').send(404, "Unable to locate an rss feed at this location.");
|
||||||
|
} else {
|
||||||
|
res.type('xml').set('Content-Length', data.length).send(data);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!fs.existsSync(rssPath)) {
|
if (!fs.existsSync(rssPath)) {
|
||||||
feed.updateTopic(tid, function (err) {
|
feed.updateTopic(tid, function (err) {
|
||||||
if (err) res.redirect('/404');
|
if (err) {
|
||||||
else loadFeed();
|
res.redirect('/404');
|
||||||
|
} else {
|
||||||
|
loadFeed();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else loadFeed();
|
} else {
|
||||||
|
loadFeed();
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -414,8 +437,9 @@ var express = require('express'),
|
|||||||
function (next) {
|
function (next) {
|
||||||
topics.getTopicWithPosts(tid, ((req.user) ? req.user.uid : 0), 0, -1, function (err, topicData) {
|
topics.getTopicWithPosts(tid, ((req.user) ? req.user.uid : 0), 0, -1, function (err, topicData) {
|
||||||
if (topicData) {
|
if (topicData) {
|
||||||
if (topicData.deleted === '1' && topicData.expose_tools === 0)
|
if (topicData.deleted === '1' && topicData.expose_tools === 0) {
|
||||||
return next(new Error('Topic deleted'), null);
|
return next(new Error('Topic deleted'), null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
next(err, topicData);
|
next(err, topicData);
|
||||||
@@ -428,7 +452,9 @@ var express = require('express'),
|
|||||||
|
|
||||||
for (var x = 0, numPosts = topicData.posts.length; x < numPosts; x++) {
|
for (var x = 0, numPosts = topicData.posts.length; x < numPosts; x++) {
|
||||||
timestamp = parseInt(topicData.posts[x].timestamp, 10);
|
timestamp = parseInt(topicData.posts[x].timestamp, 10);
|
||||||
if (timestamp > lastMod) lastMod = timestamp;
|
if (timestamp > lastMod) {
|
||||||
|
lastMod = timestamp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.build_header({
|
app.build_header({
|
||||||
@@ -481,14 +507,17 @@ var express = require('express'),
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
], function (err, data) {
|
], function (err, data) {
|
||||||
if (err) return res.redirect('404');
|
if (err) {
|
||||||
|
return res.redirect('404');
|
||||||
|
}
|
||||||
|
|
||||||
var topic_url = tid + (req.params.slug ? '/' + req.params.slug : '');
|
var topic_url = tid + (req.params.slug ? '/' + req.params.slug : '');
|
||||||
|
|
||||||
res.send(
|
res.send(
|
||||||
data.header +
|
data.header +
|
||||||
'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/topic'].parse(data.topics) + '\n\t</noscript>' +
|
'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/topic'].parse(data.topics) + '\n\t</noscript>' +
|
||||||
'\n\t<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' +
|
'\n\t<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' +
|
||||||
templates['footer']
|
templates.footer
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -501,18 +530,26 @@ var express = require('express'),
|
|||||||
var rssPath = path.join(__dirname, '../', 'feeds/categories', cid + '.rss'),
|
var rssPath = path.join(__dirname, '../', 'feeds/categories', cid + '.rss'),
|
||||||
loadFeed = function () {
|
loadFeed = function () {
|
||||||
fs.readFile(rssPath, function (err, data) {
|
fs.readFile(rssPath, function (err, data) {
|
||||||
if (err) res.type('text').send(404, "Unable to locate an rss feed at this location.");
|
if (err) {
|
||||||
else res.type('xml').set('Content-Length', data.length).send(data);
|
res.type('text').send(404, "Unable to locate an rss feed at this location.");
|
||||||
|
} else {
|
||||||
|
res.type('xml').set('Content-Length', data.length).send(data);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!fs.existsSync(rssPath)) {
|
if (!fs.existsSync(rssPath)) {
|
||||||
feed.updateCategory(cid, function (err) {
|
feed.updateCategory(cid, function (err) {
|
||||||
if (err) res.redirect('/404');
|
if (err) {
|
||||||
else loadFeed();
|
res.redirect('/404');
|
||||||
|
} else {
|
||||||
|
loadFeed();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else loadFeed();
|
} else {
|
||||||
|
loadFeed();
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -522,9 +559,11 @@ var express = require('express'),
|
|||||||
categories.getCategoryById(cid, 0, function (err, categoryData) {
|
categories.getCategoryById(cid, 0, function (err, categoryData) {
|
||||||
|
|
||||||
if (categoryData) {
|
if (categoryData) {
|
||||||
if (categoryData.disabled === '1')
|
if (categoryData.disabled === '1') {
|
||||||
return next(new Error('Category disabled'), null);
|
return next(new Error('Category disabled'), null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
next(err, categoryData);
|
next(err, categoryData);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -561,14 +600,17 @@ var express = require('express'),
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
], function (err, data) {
|
], function (err, data) {
|
||||||
if (err) return res.redirect('404');
|
if (err) {
|
||||||
|
return res.redirect('404');
|
||||||
|
}
|
||||||
|
|
||||||
var category_url = cid + (req.params.slug ? '/' + req.params.slug : '');
|
var category_url = cid + (req.params.slug ? '/' + req.params.slug : '');
|
||||||
|
|
||||||
res.send(
|
res.send(
|
||||||
data.header +
|
data.header +
|
||||||
'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/category'].parse(data.categories) + '\n\t</noscript>' +
|
'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/category'].parse(data.categories) + '\n\t</noscript>' +
|
||||||
'\n\t<script>templates.ready(function(){ajaxify.go("category/' + category_url + '");});</script>' +
|
'\n\t<script>templates.ready(function(){ajaxify.go("category/' + category_url + '");});</script>' +
|
||||||
templates['footer']
|
templates.footer
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -578,7 +620,7 @@ var express = require('express'),
|
|||||||
req: req,
|
req: req,
|
||||||
res: res
|
res: res
|
||||||
}, function (err, header) {
|
}, function (err, header) {
|
||||||
res.send(header + '<script>templates.ready(function(){ajaxify.go("confirm/' + req.params.code + '");});</script>' + templates['footer']);
|
res.send(header + '<script>templates.ready(function(){ajaxify.go("confirm/' + req.params.code + '");});</script>' + templates.footer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -599,19 +641,21 @@ var express = require('express'),
|
|||||||
|
|
||||||
app.get('/cid/:cid', function (req, res) {
|
app.get('/cid/:cid', function (req, res) {
|
||||||
categories.getCategoryData(req.params.cid, function (err, data) {
|
categories.getCategoryData(req.params.cid, function (err, data) {
|
||||||
if (data)
|
if (data) {
|
||||||
res.send(data);
|
res.send(data);
|
||||||
else
|
} else {
|
||||||
res.send(404, "Category doesn't exist!");
|
res.send(404, "Category doesn't exist!");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/tid/:tid', function (req, res) {
|
app.get('/tid/:tid', function (req, res) {
|
||||||
topics.getTopicData(req.params.tid, function (data) {
|
topics.getTopicData(req.params.tid, function (data) {
|
||||||
if (data)
|
if (data) {
|
||||||
res.send(data);
|
res.send(data);
|
||||||
else
|
} else {
|
||||||
res.send(404, "Topic doesn't exist!");
|
res.send(404, "Topic doesn't exist!");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -621,22 +665,25 @@ var express = require('express'),
|
|||||||
req: req,
|
req: req,
|
||||||
res: res
|
res: res
|
||||||
}, function (err, header) {
|
}, function (err, header) {
|
||||||
res.send(header + app.create_route("recent/" + req.params.term, null, "recent") + templates['footer']);
|
res.send(header + app.create_route("recent/" + req.params.term, null, "recent") + templates.footer);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/pid/:pid', function (req, res) {
|
app.get('/pid/:pid', function (req, res) {
|
||||||
posts.getPostData(req.params.pid, function (data) {
|
posts.getPostData(req.params.pid, function (data) {
|
||||||
if (data)
|
if (data) {
|
||||||
res.send(data);
|
res.send(data);
|
||||||
else
|
} else {
|
||||||
res.send(404, "Post doesn't exist!");
|
res.send(404, "Post doesn't exist!");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/outgoing', function (req, res) {
|
app.get('/outgoing', function (req, res) {
|
||||||
if (!req.query.url) return res.redirect('/404');
|
if (!req.query.url) {
|
||||||
|
return res.redirect('/404');
|
||||||
|
}
|
||||||
|
|
||||||
app.build_header({
|
app.build_header({
|
||||||
req: req,
|
req: req,
|
||||||
@@ -645,30 +692,34 @@ var express = require('express'),
|
|||||||
res.send(
|
res.send(
|
||||||
header +
|
header +
|
||||||
'\n\t<script>templates.ready(function(){ajaxify.go("outgoing?url=' + encodeURIComponent(req.query.url) + '", null, null, true);});</script>' +
|
'\n\t<script>templates.ready(function(){ajaxify.go("outgoing?url=' + encodeURIComponent(req.query.url) + '", null, null, true);});</script>' +
|
||||||
templates['footer']
|
templates.footer
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/search', function (req, res) {
|
app.get('/search', function (req, res) {
|
||||||
if (!req.user)
|
if (!req.user) {
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
}
|
||||||
|
|
||||||
app.build_header({
|
app.build_header({
|
||||||
req: req,
|
req: req,
|
||||||
res: res
|
res: res
|
||||||
}, function (err, header) {
|
}, function (err, header) {
|
||||||
res.send(header + app.create_route("search", null, "search") + templates['footer']);
|
res.send(header + app.create_route("search", null, "search") + templates.footer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/search/:term', function (req, res) {
|
app.get('/search/:term', function (req, res) {
|
||||||
if (!req.user)
|
if (!req.user) {
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
}
|
||||||
|
|
||||||
app.build_header({
|
app.build_header({
|
||||||
req: req,
|
req: req,
|
||||||
res: res
|
res: res
|
||||||
}, function (err, header) {
|
}, function (err, header) {
|
||||||
res.send(header + app.create_route("search/" + req.params.term, null, "search") + templates['footer']);
|
res.send(header + app.create_route("search/" + req.params.term, null, "search") + templates.footer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -713,7 +764,7 @@ var express = require('express'),
|
|||||||
req: options.req,
|
req: options.req,
|
||||||
res: options.res
|
res: options.res
|
||||||
}, function (err, header) {
|
}, function (err, header) {
|
||||||
res.send(header + options.content + templates['footer']);
|
res.send(header + options.content + templates.footer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user