Objective
Now that I can find it and come and see this article,
Then certainly know why the background to configure Browserhistory,
And I'm sure you know why you have to go with a relatively more troublesome browserhistory,
Instead of a simpler hashhistory that doesn't require a background configuration,
And the difference between Browserhistory and hashhistory.
Well, if you don't know, you can go and search,
Lots and lots. There's not much to explain here,
Nonsense not much to say, directly on the code!
Body
I am a KOA2 project built with scaffolding koa-generator. The front end is made with react + ANTD, and the route is browserhistory.
1, first we need to download a dependency connect-history-api-fallback
2. Introduce dependencies in the root directory app.js.
const connectHistory = require(‘connect-history-api-fallback‘);app.use(() => { const middleware = connectHistory(); const noop = () => { }; return async (ctx, next)=> { middleware(ctx, null, noop); await next(); };});
PS: Here is actually writing a middleware,
The code here must be placed before using other middleware,
If you do not know the location, then you will be placed before other app.use ().
Connecthistory can also be passed into the configuration,
For example, in the console display the forwarded log connecthistory ({verbose:true}) can be found on the official website.
Nodejs (KOA2) configuration browserhistory