Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement custom routes? #434

Closed
jedfoster opened this issue Jun 16, 2020 · 8 comments · Fixed by #439
Closed

How to implement custom routes? #434

jedfoster opened this issue Jun 16, 2020 · 8 comments · Fixed by #439
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@jedfoster
Copy link
Contributor

I'm trying to figure out how to implement custom routes using json-server's rewriter.

I have tried this:

server.use(jsonServer.rewriter({
  '/blog/:resource/:id/show': '/:resource/:id',
  '/articles?id=:id': '/posts/:id'
}))

let core: CoreApp | undefined;

But I get 404 Not Found from http://localhost:3000/dev/api/blog/posts/1/show (I'm using the example JSON from the json-server readme) http://localhost:3000/dev/api/posts/1 works just fine.

What am I doing wrong?


On a related note, it would be nice if the api/ path could be configured from appconfig.json. I may put in a PR for that change.

@pharindoko
Copy link
Owner

pharindoko commented Jun 16, 2020

Hey @jedfoster,

I didn‘t try out the custom routes. But now I‘m interested to see why it doesn‘t work.
Will check it tomorrow.

PRs are always welcome. there‘s room for improvements. 👍

@pharindoko pharindoko pinned this issue Jun 16, 2020
@pharindoko pharindoko self-assigned this Jun 20, 2020
@pharindoko pharindoko linked a pull request Jun 20, 2020 that will close this issue
@pharindoko pharindoko added the enhancement New feature or request label Jun 20, 2020
@pharindoko
Copy link
Owner

Hey @jedfoster,

I tested it - your missing the apiRoutePath in this case /api in this mapping

server.use(
  jsonserver.rewriter({
    '/articles?id=:id': '/api/posts/:id',
  })
);
```

@pharindoko pharindoko added the question Further information is requested label Jun 21, 2020
@jedfoster
Copy link
Contributor Author

Sure enough. I thought I had tried every possible permutation, but apparently I missed one.

Changed my rewrite rule to:

server.use(
  jsonServer.rewriter({
    '/blog/:resource/:id/show': '/api/:resource/:id',
    '/articles?id=:id': '/api/posts/:id'
  })
);

And now http://localhost:3000/dev/blog/posts/1/show works perfectly.

@scafmac
Copy link

scafmac commented Mar 9, 2021

I didn't want to open a new issue, since I expect I'm missing something, but I can't get the rewriter example to work. I also am trying to figure out how exactly to customize the response object as described in this custom output/response example - any chance there's an example of that for json-serverless around?

I tried a few variations on including api for the custom routes, but they continued to return a 404. The target paths work fine. Here's my code from handler.ts

...
const jsonServer = require("json-server");
const server = express();

server.use(
	jsonServer.rewriter({
		"/clientService/client/*": "/api/clientService-client/$1",
		"/clientService/nav/*": "/api/clientService-nav/$1"
	})
);

const sls = serverlessHttp(server);
...

Anything obvious stand out to you?

Thanks for json-serverless. I'm excited to use it.

@pharindoko pharindoko reopened this Mar 9, 2021
@pharindoko
Copy link
Owner

Hey @scafmac,

I don‘t have a direct answer to that. I will check it tomorrow.

br,

@pharindoko

@pharindoko
Copy link
Owner

@scafmac
this seems to be tricky.
I tried again the rewriter @jedfoster mentioned again and this worked.

but I couldn`t even use this rewriter locally

server.use(
  jsonServer.rewriter({
    'api/*': '/$1',
  })
);

need to test tomorrow if it works with json-server itself....

br,

@pharindoko

@pharindoko
Copy link
Owner

@scafmac sorry but I haven`t found the issue.
Does this work with json server but not with json-serverless itself ?
Please provide a small example - would be helpful for testing.

br,

@pharindoko

@pharindoko
Copy link
Owner

I will close this issue now as I did not receive a further reply.
Can`t solve this yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants