Skip to content

Node.js based forum software built for the modern web

Home route (#7039)

Barış Soner Uşaklı1f918ca8f85e
diff --git a/.travis.yml b/.travis.yml
index 696b9673b6..178fe2b63d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,7 +8,7 @@ before_script:
- sleep 15 # wait for mongodb to be ready
- "mongo mydb_test --eval 'db.createUser({user:\"travis\", pwd: \"test\", roles: []});'"
- sh -c "if [ '$DB' = 'mongodb' ]; then node app --setup=\"{\\\"url\\\":\\\"http://127.0.0.1:4567\\\",\\\"secret\\\":\\\"abcdef\\\",\\\"database\\\":\\\"mongo\\\",\\\"mongo:host\\\":\\\"127.0.0.1\\\",\\\"mongo:port\\\":27017,\\\"mongo:username\\\":\\\"\\\",\\\"mongo:password\\\":\\\"\\\",\\\"mongo:database\\\":0,\\\"admin:username\\\":\\\"admin\\\",\\\"admin:email\\\":\\\"[email protected]\\\",\\\"admin:password\\\":\\\"hAN3Eg8W\\\",\\\"admin:password:confirm\\\":\\\"hAN3Eg8W\\\"}\" --ci=\"{\\\"host\\\":\\\"127.0.0.1\\\",\\\"port\\\":27017,\\\"database\\\":0}\"; fi"
- - sh -c "if [ '$DB' = 'redis' ]; then node app --setup=\"{\\\"url\\\":\\\"http://127.0.0.1:4567\\\",\\\"secret\\\":\\\"abcdef\\\",\\\"database\\\":\\\"redis\\\",\\\"redis:host\\\":\\\"127.0.0.1\\\",\\\"redis:port\\\":6379,\\\"redis:password\\\":\\\"\\\",\\\"redis:database\\\":0,\\\"admin:username\\\":\\\"admin\\\",\\\"admin:email\\\":\\\"[email protected]\\\",\\\"admin:password\\\":\\\"hAN3Eg8W\\\",\\\"admin:password:confirm\\\":\\\"hAN3Eg8W\\\"}\" --ci=\"{\\\"host\\\":\\\"127.0.0.1\\\",\\\"port\\\":6379,\\\"database\\\":0}\"; fi"
+ - sh -c "if [ '$DB' = 'redis' ]; then node app --setup=\"{\\\"url\\\":\\\"http://127.0.0.1:4567/forum\\\",\\\"secret\\\":\\\"abcdef\\\",\\\"database\\\":\\\"redis\\\",\\\"redis:host\\\":\\\"127.0.0.1\\\",\\\"redis:port\\\":6379,\\\"redis:password\\\":\\\"\\\",\\\"redis:database\\\":0,\\\"admin:username\\\":\\\"admin\\\",\\\"admin:email\\\":\\\"[email protected]\\\",\\\"admin:password\\\":\\\"hAN3Eg8W\\\",\\\"admin:password:confirm\\\":\\\"hAN3Eg8W\\\"}\" --ci=\"{\\\"host\\\":\\\"127.0.0.1\\\",\\\"port\\\":6379,\\\"database\\\":0}\"; fi"
- sh -c "if [ '$DB' = 'postgres' ]; then psql -c 'create database nodebb;' -U postgres; psql -c 'create database travis_ci_test;' -U postgres; node app --setup=\"{\\\"url\\\":\\\"http://127.0.0.1:4567\\\",\\\"secret\\\":\\\"abcdef\\\",\\\"database\\\":\\\"postgres\\\",\\\"postgres:host\\\":\\\"127.0.0.1\\\",\\\"postgres:port\\\":5432,\\\"postgres:password\\\":\\\"\\\",\\\"postgres:database\\\":\\\"nodebb\\\",\\\"admin:username\\\":\\\"admin\\\",\\\"admin:email\\\":\\\"[email protected]\\\",\\\"admin:password\\\":\\\"hAN3Eg8W\\\",\\\"admin:password:confirm\\\":\\\"hAN3Eg8W\\\"}\" --ci=\"{\\\"host\\\":\\\"127.0.0.1\\\",\\\"port\\\":5432,\\\"username\\\":\\\"postgres\\\",\\\"database\\\":\\\"travis_ci_test\\\"}\"; fi"
after_success:
- "npm run coveralls"
diff --git a/src/controllers/errors.js b/src/controllers/errors.js
index dacca84e3c..0887de8c9b 100644
--- a/src/controllers/errors.js
+++ b/src/controllers/errors.js
@@ -8,8 +8,9 @@ var plugins = require('../plugins');
exports.handleURIErrors = function (err, req, res, next) {
// Handle cases where malformed URIs are passed in
if (err instanceof URIError) {
- var tidMatch = req.path.match(/^\/topic\/(\d+)\//);
- var cidMatch = req.path.match(/^\/category\/(\d+)\//);
+ const cleanPath = req.path.replace(new RegExp('^' + nconf.get('relative_path')), '');
+ var tidMatch = cleanPath.match(/^\/topic\/(\d+)\//);
+ var cidMatch = cleanPath.match(/^\/category\/(\d+)\//);
if (tidMatch) {
res.redirect(nconf.get('relative_path') + tidMatch[0]);
@@ -49,7 +50,7 @@ exports.handleErrors = function (err, req, res, next) { // eslint-disable-line n
// Display NodeBB error page
var status = parseInt(err.status, 10);
if ((status === 302 || status === 308) && err.path) {
- return res.locals.isAPI ? res.set('X-Redirect', err.path).status(200).json(err.path) : res.redirect(err.path);
+ return res.locals.isAPI ? res.set('X-Redirect', err.path).status(200).json(err.path) : res.redirect(nconf.get('relative_path') + err.path);
}
winston.error(req.path + '\n', err);
diff --git a/src/controllers/ping.js b/src/controllers/ping.js
index d43beca298..6c4dc7f697 100644
--- a/src/controllers/ping.js
+++ b/src/controllers/ping.js
@@ -1,7 +1,8 @@
'use strict';
-var async = require('async');
-var db = require('../database');
+const async = require('async');
+const nconf = require('nconf');
+const db = require('../database');
module.exports.ping = function (req, res, next) {
async.waterfall([
@@ -9,7 +10,7 @@ module.exports.ping = function (req, res, next) {
db.getObject('config', next);
},
function () {
- res.status(200).send(req.path === '/sping' ? 'healthy' : '200');
+ res.status(200).send(req.path === nconf.get('relative_path') + '/sping' ? 'healthy' : '200');
},
], next);
};
diff --git a/src/routes/index.js b/src/routes/index.js
index 5775f1c62c..71d4ac63b2 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -87,18 +87,7 @@ function groupRoutes(app, middleware, controllers) {
}
module.exports = function (app, middleware, callback) {
- var routers = [
- express.Router(), // plugin router
- express.Router(), // main app router
- express.Router(), // auth router
- ];
- var pluginRouter = routers[0];
- pluginRouter.render = function () {
- app.render.apply(app, arguments);
- };
-
- var router = routers[1];
- var authRouter = routers[2];
+ const router = express.Router();
var relativePath = nconf.get('relative_path');
var ensureLoggedIn = require('connect-ensure-login');
@@ -111,11 +100,25 @@ module.exports = function (app, middleware, callback) {
// handle custom homepage routes
router.use('/', controllers.home.rewrite);
- pluginRouter.use('/', controllers.home.rewrite);
// homepage handled by `action:homepage.get:[route]`
setupPageRoute(router, '/', middleware, [], controllers.home.pluginHook);
+ async.series([
+ async.apply(plugins.reloadRoutes, router),
+ async.apply(authRoutes.reloadRoutes, router),
+ async.apply(addCoreRoutes, app, router, middleware),
+ async.apply(user.addInterstitials),
+ function (next) {
+ winston.info('Routes added');
+ next();
+ },
+ ], function (err) {
+ callback(err);
+ });
+};
+
+function addCoreRoutes(app, router, middleware, callback) {
adminRoutes(router, middleware, controllers);
metaRoutes(router, middleware, controllers);
apiRoutes(router, middleware, controllers);
@@ -132,9 +135,8 @@ module.exports = function (app, middleware, callback) {
userRoutes(router, middleware, controllers);
groupRoutes(router, middleware, controllers);
- routers.forEach((router) => {
- app.use(relativePath || '/', router);
- });
+ var relativePath = nconf.get('relative_path');
+ app.use(relativePath || '/', router);
if (process.env.NODE_ENV === 'development') {
require('./debug')(app, middleware, controllers);
@@ -184,17 +186,5 @@ module.exports = function (app, middleware, callback) {
app.use(controllers['404'].handle404);
app.use(controllers.errors.handleURIErrors);
app.use(controllers.errors.handleErrors);
-
- // Add plugin routes
- async.series([
- async.apply(plugins.reloadRoutes, pluginRouter),
- async.apply(authRoutes.reloadRoutes, authRouter),
- async.apply(user.addInterstitials),
- function (next) {
- winston.info('Routes added');
- next();
- },
- ], function (err) {
- callback(err);
- });
-};
+ setImmediate(callback);
+}
diff --git a/test/controllers.js b/test/controllers.js
index 53cdd39788..16a2dd5a2b 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -2144,7 +2144,7 @@ describe('Controllers', function () {
assert.equal(res.statusCode, 200);
assert(body.title);
assert(body.template);
- assert.equal(body.url, '/compose');
+ assert.equal(body.url, nconf.get('relative_path') + '/compose');
done();
});
});
@@ -2165,7 +2165,7 @@ describe('Controllers', function () {
assert.equal(res.statusCode, 200);
assert(body.title);
assert.strictEqual(body.template.name, '');
- assert.strictEqual(body.url, '/compose');
+ assert.strictEqual(body.url, nconf.get('relative_path') + '/compose');
plugins.unregisterHook('myTestPlugin', 'filter:composer.build', hookMethod);
done();
diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js
index c5c3726dd7..86014dce6b 100644
--- a/test/mocks/databasemock.js
+++ b/test/mocks/databasemock.js
@@ -33,6 +33,10 @@ nconf.defaults({
relative_path: '',
});
+var urlObject = url.parse(nconf.get('url'));
+var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
+nconf.set('relative_path', relativePath);
+
if (!nconf.get('isCluster')) {
nconf.set('isPrimary', 'true');
nconf.set('isCluster', 'true');
diff --git a/test/uploads.js b/test/uploads.js
index 092813108a..077ce55704 100644
--- a/test/uploads.js
+++ b/test/uploads.js
@@ -95,7 +95,7 @@ describe('Upload Controllers', function () {
assert.equal(res.statusCode, 200);
assert(Array.isArray(body));
assert(body[0].url);
- var name = body[0].url.replace(nconf.get('upload_url'), '');
+ var name = body[0].url.replace(nconf.get('relative_path') + nconf.get('upload_url'), '');
socketUser.deleteUpload({ uid: regularUid }, { uid: regularUid, name: name }, function (err) {
assert.ifError(err);
db.getSortedSetRange('uid:' + regularUid + ':uploads', 0, -1, function (err, uploads) {