diff --git a/server/route.js b/server/route.js index bce2ce19..5800109e 100644 --- a/server/route.js +++ b/server/route.js @@ -23,10 +23,11 @@ const prefix = squad(prefixer, apart(config, 'prefix')); const isDev = process.env.NODE_ENV === 'development'; const getDist = (isDev) => isDev ? 'dist-dev' : 'dist'; -const getIndexPath = (isDev) => { +module.exports._getIndexPath = getIndexPath; +function getIndexPath(isDev) { const dist = getDist(isDev); return path.join(DIR, `${dist}/index.html`); -}; +} const FS = CloudFunc.FS; @@ -102,6 +103,17 @@ function indexProcessing(options) { return data; } +const sendIndex = (params) => (error, data) => { + const ponseParams = Object.assign({}, params, { + name: getIndexPath(isDev) + }); + + if (error) + return ponse.sendError(error, ponseParams); + + ponse.send(data, ponseParams); +}; + /** * routing of server queries */ @@ -130,14 +142,7 @@ function route(request, response, callback) { dir.path = format.addSlashToEnd(name); if (!error) - return buildIndex(dir, (error, data) => { - p.name = getIndexPath(isDev); - - if (error) - return ponse.sendError(error, p); - - ponse.send(data, p); - }); + return buildIndex(dir, sendIndex(p)); if (error.code !== 'ENOTDIR') return ponse.sendError(error, p); @@ -170,7 +175,7 @@ function buildIndex(json, callback) { data: template, }); - callback(error, data); + callback(null, data); }); } diff --git a/test/server/route.js b/test/server/route.js index 13b6cd65..3da1543c 100644 --- a/test/server/route.js +++ b/test/server/route.js @@ -12,6 +12,11 @@ const rootDir = '../..'; const routePath = `${rootDir}/server/route`; const beforePath = '../before'; const configPath = `${rootDir}/server/config`; +const cloudcmdPath = `${rootDir}/server/cloudcmd`; + +const { + _getIndexPath, +} = require(routePath); const route = require(routePath); const before = require(beforePath); @@ -300,3 +305,31 @@ test('cloudcmd: route: realpath: error', (t) => { }); }); +test('cloudcmd: route: sendIndex: error', (t) => { + const error = Error('index path error'); + const {readFile} = fs; + const isDev = true; + const indexPath = _getIndexPath(isDev); + + fs.readFile = (name, options, fn = options) => { + if (name === indexPath) { + fn(error); + fs.readFile = readFile;; + return; + }; + + return readFile(name, options, fn); + }; + + const config = {}; + + before({config}, (port, after) => { + getStr(`http://localhost:${port}`) + .then((data) => { + t.equal(data, error.message, 'should return error'); + t.end(); + after(); + }) + }); +}); +