Egg.js middleware doesn't work?

I write authentication interface in middleware

authenticate.js
module.exports = options => {
    return async function auth(ctx, next) {
        console.log("99999999999999999")
        let { app } = this
        const { redis } = app;
        let sign = ctx.get("x-sign");
        let user_id = ctx.get("x-user-id");

        if (!user_id) {
            ctx.throw(412, "invalid user id");
        }
        // if (!sign) {
        //     ctx.throw(412, "invalid sign");
        // }
        let token = await redis.get(user_id);
        if (!token) {
            ctx.throw(412, "invalid user");
        }
        await next();
        console.log("9999999999999")
    };
};

load middleware in config.default.js

config.middleware = ["authenticate"]

use psotman to request the interface, but do not use this middleware at all. What is the reason?

I disabled csrf

Mar.17,2022

found the problem. I added the config.local.js configuration file to the config file, in which middleware can be added, but I saw in the official documentation that config.default.js will be loaded anyway, so why not configure it here?

found the problem, because middleware, is also configured in config.local.js, which will overwrite the middleware configuration in config.default.js, so the configuration in config.defalut.js does not work. The loading rule of the configuration file is that the configuration files of other environments will overwrite the same configuration items in config.default.js

.

specially register an account to thank you. After debugging for a long time, I suddenly realized when I saw you. Thank you < del > ~ < / del > ~

Menu