can't seem to get static route with kraken.js

942 views Asked by At

I'm trying to upgrade an existing express site to use kraken.js

I've got my dynamic pages loading ok (so far), but I can't seem to serve static files.

Looking at the example, pages, it seems simple enough that I just have to add

"middleware": {
    "static": {
        "arguments": [ "path: ./client" ]
    }
}

In my config.json file. The file I'm trying to serve is ./client/build/js/bundle.js, and I can confirm that the file exists in the folder. It is NOT in a ./public folder.

What do I need to do to get kraken (or kraken.js static-serve) to find my static files?

I've placed the file in a ./public/client/build/js/bundle.js and kraken has no problem finding the file in that location.

2

There are 2 answers

1
HeadCode On

I think you might be missing the "module" member of the middleware object. My current Kraken-generated static middleware config object looks like this:

"static": {
    "module": {
        "arguments": [ "path:./.build" ]
    }
}

OK, I found out how to make it work. Notice how in your config that "public" isn't in there? That meant it was being pre-pended or configured somewhere else. That somewhere else is in /node-modules/kraken-js/config/config.json. I amended it to look like this:

"static": {
    "enabled": true,
    "priority": 40,
    "module": {
        "name": "serve-static",
            "arguments": [ "path:./public", "path:./client" ]
    }
},

Then in your regular /config/config.json I edited the static object to look like this:

"static": {
    "module": {
        "arguments": [ "path:./.build", "path:./build" ]
    }
},

Notice that in the second argument there is not a "." before build.

Finally, I used script tags that looked like this in the master layout:

<script src="/js/test.js"></script>

So, from your root project directory I have /client/build/js/test.js and test.js loads correctly.

Also, there is one more way to do it that is easier and doesn't require mucking about in the kraken source. In your main index.js file you can do this:

app = module.exports = express();
app.use(kraken(options));
app.use(express.static('client'));  // Add this line
0
pedalpete On

Days, then weeks, then months went by, and nothing worked. I should have noted that I am not using Yoeman, and we use gulp at work, which may have been part of the problem.

The solution turned out to be

"middleware": { "static": { "route": "/public" },

which looks very different to any of the documentation from kraken. Hope this helps somebody.