Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@duffpod
Last active February 8, 2018 14:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duffpod/746a660bcddfd986878c92dde1a04f06 to your computer and use it in GitHub Desktop.
Save duffpod/746a660bcddfd986878c92dde1a04f06 to your computer and use it in GitHub Desktop.
Sails.js + express-vue SSR
  1. Generate a Sails project with sails new your-app --no-frontend
  2. Install express-vue with npm install express-vue --save
  3. Go to the view config of Sails app at your-app/config/views.js and replace the engine: 'ejs', with this:
  engine: {
    ext: 'vue',
    fn: (function() {
      return require('express-vue');
    })()
  },

Also need to set the layout: 'layout', to layout: false, as it will be ignored by Sails anyway.

  1. Now we need to create views folder in the Sails app. mkdir your-app/views/ && touch your-app/views/homepage.vue. Note, that your-app/config/routes.js is already pointing the homepage-file to the localhost:1337/.
  2. Edit the homepage.vue to add your Vue.js content to it, like:
<template>
  <div>
    <h1>Hello, world!</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
};
</script>
  1. After all, go to the config/http.js in your project. There will be the middleware object. You need to add the customMiddleware function AFTER it, so everything looks like this:
module.exports.http = {
  middleware: {

  },
  customMiddleware: function(app) {
    app.set('vue', {
      // configure express-vue here
      // do not use __dirname here, otherwise the path will look like: 
      // /Users/username/your-project/config/components
      // componentsDir: app.settings.views + '/components',
    });
  }
};
@steventhanna
Copy link

In version 4 of express-vue, this method results in the following:

error: A hook (`http`) failed to load!
error: Error: callback function required
    at Function.app.engine (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/application.js:207:38)
    at /usr/local/lib/node_modules/sails/lib/hooks/http/initialize.js:131:22
    at /usr/local/lib/node_modules/sails/lib/app/private/after.js:91:14
    at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:721:13
    at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:52:16
    at async.forEachOf.async.eachOf (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:236:30)
    at _parallel (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:712:9)
    at Object.async.parallel (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:726:9)
    at Sails.emitter.after (/usr/local/lib/node_modules/sails/lib/app/private/after.js:89:11)
    at _afterLoadingSessionHookIfApplicable (/usr/local/lib/node_modules/sails/lib/hooks/http/initialize.js:121:15)
    at /usr/local/lib/node_modules/sails/lib/hooks/http/initialize.js:31:18
    at /usr/local/lib/node_modules/sails/lib/app/private/after.js:91:14
    at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:721:13
    at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:52:16
    at async.forEachOf.async.eachOf (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:236:30)
    at _parallel (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:712:9)
    at Object.async.parallel (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:726:9)
    at Sails.emitter.after (/usr/local/lib/node_modules/sails/lib/app/private/after.js:89:11)
    at _waitForSessionHookIfApplicable (/usr/local/lib/node_modules/sails/lib/hooks/http/initialize.js:30:15)
    at Hook.initialize (/usr/local/lib/node_modules/sails/lib/hooks/http/initialize.js:42:7)
    at Hook.wrapper [as initialize] (/usr/local/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19)
    at /usr/local/lib/node_modules/sails/lib/hooks/index.js:88:16

The express-vue docs now just say

This is middleware now so support for sails should just work as middleware.

But there is no example of actually configuring express-vue in SailsJS. I have tried every combination of settings that I can think of in order to get something operational, but everything results in the above error. Any insight into this would be extremely valuable.

My guess so far is that it has something to do with the configuration of the engine, as without it the system starts (but is unable to render anything).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment