2
votes

I've been following the instructions for how to set up a Node.js application using Loopback as described onToptal and Developing APIs with Node.js and LoopBack.io. Following these steps, I get a UrlNotFoundError when I try to access the REST API for the application.

Steps to reproduce:

  1. Installed the Strongloop command line: $ npm install -g strongloop
  2. Run the Loopback's application generator: $ slc loopback.
  3. Run the project: $ node . The terminal displays the following
    Web server listening at: http://0.0.0.0:3000 Browse your REST API at http://0.0.0.0:3000/explorer
  4. Browse the REST API.
  5. Try to POST to /Users with the provided model.

This results in the following response:

{
  "error": {
    "statusCode": 404,
    "name": "Error",
    "message": "Cannot POST /api/Users",
    "status": 404,
    "stack": "Error: Cannot POST /api/Users\n    at raiseUrlNotFoundError (/Repos/loopback-test/node_modules/loopback/server/middleware/url-not-found.js:20:17)\n    at Layer.handle [as handle_request] (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)\n    at trim_prefix (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:312:13)\n    at /Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:280:7\n    at Function.process_params (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:330:12)\n    at next (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:271:10)\n    at /loopback-test/node_modules/loopback/node_modules/async/dist/async.js:486:20\n    at replenish (/Repos/loopback-test/node_modules/loopback/node_modules/async/dist/async.js:879:29)\n    at /loopback-test/node_modules/loopback/node_modules/async/dist/async.js:888:13\n    at eachLimit$1 (/Repos/loopback-test/node_modules/loopback/node_modules/async/dist/async.js:3136:26)\n    at Object.<anonymous> (/Repos/loopback-test/node_modules/loopback/node_modules/async/dist/async.js:920:20)\n    at restApiHandler (/Repos/loopback-test/node_modules/loopback/server/middleware/rest.js:63:11)\n    at Layer.handle [as handle_request] (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)\n    at trim_prefix (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:312:13)\n    at /Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:280:7\n    at Function.process_params (/Repos/loopback-test/node_modules/loopback/node_modules/express/lib/router/index.js:330:12)"
  }
}

What I have tried:

  • Setting up and connecting to a database, as per the instructions in the Toptal post.
  • Tried the API project and sample Notes project, when setting up the Loopback project (step 2 above).
  • Compared the project to another Loopback project I'm working on, where the REST API works. Unfortunately, I'm not experienced enough to spot any significant difference.
  • Implement a custom model (as seen in both the Toptal post and Youtube video) and trying to access it via the REST API.
  • Searched for the raiseUrlNotFoundError. The best hit is this issue on GitHub which provides a (dead) link to a SO question.
  • Tried running the project on a different port than the standard 3000, in case there was some issue with other projects.
  • Setup automigration of the common models to the datebase. Added a user and an accesstoken to the database and tried to use that accesstoken in the REST API Explorer.

I run Mac OS X, version 10.11.6. I have Node, npm and other necessary tools installed.

What am I missing, or what can I do to avoid getting the raiseUrlNotFoundError, and access the REST API for my project?

3

3 Answers

2
votes

I know it's not a really good solution but it's something (using win 10 64 bit). I got exactly the same error while creating the app slc loopback and selecting Loopback version 3.X , when recreated the app and selecting Loopback version 2.X the api works fine , probably that's the different between your projects.

1
votes

To make it run just install the api-explorer

npm install --save loopback-component-explorer --save

and register the route as follows:

in the directory-structure from root-folder there is a folder called "server". Create an empty file named "component-config.json" into the "server"-folder and place the following code inside:

{
  "loopback-component-explorer": {
    "mountPath": "/explorer"
  }
}

Now you should be able to open the explorer-page with the url http://0.0.0.0:3000/explorer.

1
votes

That problem is fixed with loopback 3.x, so please give it a try now. If you are still having the same issue, follow these instructions:

To remove this warning, disable the context middleware added by the built-in REST handler. Set the remoting.context property in server/config.json to false;

    {
  "remoting": {
    "context": false,
    // etc.
  },
  // etc.
}

for more info: http://loopback.io/doc/en/lb2/Using-current-context.html