lazzy_ms
lazzy_ms

Reputation: 1374

problem hosting a Static page with loopback

I want to host a static page in loopback. I have added HTML in client dir and removed the default path in server/boot/root.js

But, I have to follow some rules about routes so that I replaced '/api/' to '/' in config.json's restApiRoot. Now I want to host this static HTML page on '/' route. But It gives this error

{"success":false,"message":"There is no method to handle GET /"}

Also, I tried to set a different path for static page in middleware.json like this,

"files": {
    "loopback#static": {
      "paths": [
        "/admin/"
      ],
      "params": "$!../client"
    }
  },

Still, this is also not working. PS: I have added a boot script for following our code of conduct for the response, that's why it's giving this type of response.

Upvotes: 1

Views: 155

Answers (1)

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10785

By default, LoopBack's REST API handler takes ownership of the entire path namespace it's mounted on. If you mount your REST API at /api, then our handler will return 404 whenever a path is not mapped to any remote methods. When you mount your REST API at /, the handler will take over the entire HTTP server.

Fortunately, it's possible to disable this behavior via configuration. See rest. handleUnknownPaths option in config.json >> Remoting properties.

Here is an example showing how to disable this flag in your server/config.json file (please note I omitted any other configuration you may already have in that file):

{
  "remoting": {
    "rest": {
      "handleUnknownPaths": false
    }
  }
}

I have added a boot script for following our code of conduct for the response, that's why it's giving this type of response.

You may want to set both errorHandler and rest.handleErrors to false, so that LoopBack does not convert errors to HTTP responses.

Example server/config.json:

{
  "remoting": {
    "errorHandler": false,
    "rest": {
      "handleErrors": false,
      "handleUnknownPaths": false
    }
  }
}

With this setup in place, you can edit server/middleware.json and replace the default strong-error-handler middleware with your own error-handling implementation (see Defining middleware >> Path to middleware function.

Upvotes: 1

Related Questions