Hosting sails.js application from subdirectory

江枫思渺然 提交于 2019-12-06 00:35:10

You can configure nginx to serve the static content from that url.

location /mysailsapp/ {
    alias /path/to/your/app/assets/;
}

Prorper way of doing this is hidding in official docs for sails grunt assets-linker plugin: https://github.com/Zolmeister/grunt-sails-linker where you should use absolute path but this seems doesn't work. So you can use little hack.

Trick would be to put var that represents your subdirectory name inside tasks/linkAssets.js like in example:

var subdirectory='/dashboard';

module.exports = function(grunt) {

    grunt.config.set('sails-linker', {
        devJs: {
            options: {
                startTag: '<!--SCRIPTS-->',
                endTag: '<!--SCRIPTS END-->',
                fileTmpl: '<script src="'+prefix+'%s"></script>',
                appRoot: '.tmp/public'
            },
            files: {
                '.tmp/public/**/*.html': require('../pipeline').jsFilesToInject,
                'views/**/*.html': require('../pipeline').jsFilesToInject,
                'views/**/*.ejs': require('../pipeline').jsFilesToInject
            }
        },

        devJsRelative: {
            options: {
                startTag: '<!--SCRIPTS-->',
                endTag: '<!--SCRIPTS END-->',
                fileTmpl: '<script src="'+prefix+'%s"></script>',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                '.tmp/public/**/*.html': require('../pipeline').jsFilesToInject,
                'views/**/*.html': require('../pipeline').jsFilesToInject,
                'views/**/*.ejs': require('../pipeline').jsFilesToInject
            }
        },

and so on, i hope you get the point.

Sails (at least the version I'm using: 0.11) includes grunt tasks that link assets with the grunt-sails-linker's option "relative" set to "true", which avoids setting the initial "/" in the asset's URL. Those tasks names have the suffix "Relative", to differentiate them from their "default" counterparts that have "relative" set to "false".

For example, there is:

  • devStylesRelative
  • prodStylesRelative
  • devJsRelative
  • ... and so on

Also, there are alias tasks that run those "relative versions" instead of the "absolute ones". E.g. linkAssetsBuild

You can find these tasks being registered in tasks/config/sails-linker.js.

The solution I used, (although I'm not sure if it's the most standard one or not) is changing the default task to run linkAssetsBuild instead of linkAssets, to ensure that the tasks that are run when lifting sails are the "relative ones".

You can do this by modifying the file tasks/register/default.js.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!