Nodejs (KOA2) configuration browserhistory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.