0

I'm kind of new in react and trying to develop sample app based on reactjs + expressjs + nodejs referred by https://github.com/lmammino/judo-heroes but problem is when I'm trying to create webhook api for 3rd party application, react router showed me as 404 page.

server.js

import routes from './routes';
...
...
var webhook = require('./routes/webhook');
app.use('webhook', webhook);

and routes.js

'use strict';

import React from 'react'
import { Route, IndexRoute } from 'react-router'
import Layout from './components/Layout';
import IndexPage from './components/IndexPage';
import NotFoundPage from './components/NotFoundPage';

const routes = (
  <Route path="/" component={Layout}>
    <IndexRoute component={IndexPage}/>
    <Route path="*" component={NotFoundPage}/>
  </Route>
);

export default routes;

and when I called "localhost:3333/webhook", it gone to execute <Route path="*" component={NotFoundPage}/> as showed 404 error page.

Please help me how can I export webhook as individual api not related to react router?

2 Answers 2

1

This happens because you are trying to render client side routes in the same way you are for server side and this is causing a conflict between the routing in React + Express. I'd suggest you are probably using browserHistory and I would swap to hashHistory as this means the client side is rendered differently compared to express routes. By swapping to hash your routes in react become rendered via /#/index. This allows express to render API routes in the traditional format like /api/webhook. You can read more about histories here https://github.com/ReactTraining/react-router/blob/master/docs/API.md#histories

Hope this helps

Dylan

Sign up to request clarification or add additional context in comments.

Comments

0

You should mount your webhook router with a leading slash:

app.use('/webhook', webhook);

This should be executed before app.get('*', ... wildcard handler to prevent React app from being served and bootstrapped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.