trailsjs / trailpack-hapi

:package: Hapi.js Trailpack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple Assets - Not taking objects into account

crash83k opened this issue · comments

I'm attempting to use multiple static directories. You can use the example shown in the documentation, it will fail.

It attempts to make the DIR but doesn't check if it's an object, instead expects a string.

This will work.
www: path.resolve(__dirname, '..', 'static')

This will not.

www: [
      {
        path: path.resolve(__dirname, '..', 'static'),
        humanUrl: '/admin'
      },
      {
        path: path.resolve(__dirname, '..', 'uploads', 'pictures', 'cats'),
        humanUrl: '/cats'
      }
    ]

This will eventually get to trailpack-core (line 46)

// create paths if they don't exist
    return Promise.all(Object.keys(paths).map(pathName => {
      const dir = paths[pathName]

      try {
        const stats = fs.statSync(dir)

        if (!stats.isDirectory()) {
          this.log.error('The path "', pathName, '" is not a directory.')
          this.log.error('config.main.paths should only contain paths to directories')
          return Promise.reject()
        }
      }
      catch (e) {
        fs.mkdirSync(dir)
      }
    }))
  }

And dir will be the object, not the string. Thus, an error will be thrown killing the server.

@crash83k can you link to the documentation where you read this ? Because yes it will not work main.paths are simple key/value.

If you want multiple static directories you have to add them under config/routes like this :

[
{
    method: 'GET',
    path: '/static1',
    handler: {
      directory: {
        path: 'node_modules'
      }
    }
  },{
    method: 'GET',
    path: '/static2',
    handler: {
      directory: {
        path: 'myDir'
      }
    }
  },
]

@crash83k I just miss an update sorry :) so yes this will work but new version of trailpack-core witch this functionality is not on npm yet for for now, so it's not working but you can use what I say on previous comment.
cc @konstantinzolotarev