Add multiple dir for static files in Loopback
Asked Answered
B

1

19

Loopback has two areas where paths are set for static files:

server.js

   var path = require('path');
   app.use(loopback.static(path.resolve(__dirname, '../client')));

middleware.json

"files": {
    "loopback#static": {
      "params": "$!../client"
      }
  },

In my dev environment I'd also like to reference another dir for example /node_modules

How do I do this?

Boigie answered 25/2, 2015 at 21:24 Comment(0)
D
42

Register loopback.static multiple times in server.js:

...
app.use(loopback.static(path.resolve(__dirname, '../client')));
app.use(loopback.static(path.resolve(__dirname, '../other-dir')));
...

The first one has highest precedence. See http://expressjs.com/api.html for more info.

You can do it with phases too, inside your middleware.json (See docs):

"files": {
    "loopback#static": [{
        "name": "client",
        "paths": ["/client"],
        "params": "$!../client"
    },
    {
        "name": "someother",
        "paths": ["/someother"],
        "params": "$!../someother"
    }]
}
Definitive answered 26/2, 2015 at 5:3 Comment(3)
It work perfectly using the given description for middleware.json without modifying the server.js, ThanksOarsman
Note: [ and ] in the middleware.json method.Tricksy
How would I achieve serving a static file based on the subdomain rather than the path? I want to serve app1 for subdomain1.app.com and app2 for subdomain2.app.comPapillote

© 2022 - 2024 — McMap. All rights reserved.