Программное обеспечение NuxtJS serverMiddleware перенаправляется не так, как ожидалось

#node.js #express #nuxt.js

Вопрос:

Я сталкиваюсь с проблемами, связанными с перенаправлением, определенным в свойстве «serverMiddleware» в файле конфигурации.

У меня есть nuxt.config.js устройство, настроенное следующим образом:

 import colors from "vuetify/es5/util/colors";

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    titleTemplate: "%s - concept-slide-frontend",
    title: "concept-slide-frontend",
    htmlAttrs: {
      lang: "en"
    },
    meta: [
      { charset: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1" },
      { hid: "description", name: "description", content: "" }
    ],
    link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/vuetify
    "@nuxtjs/vuetify"
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: ["@nuxtjs/axios", "@nuxtjs/auth", "@nuxtjs/proxy"],
  auth: {
    redirect: {
      login: "/login",
      logout: "/",
      home: "/studs"
    },
    strategies: {
      local: {
        endpoints: {
          login: {
            url: "/auth/login",
            method: "post",
            propertyName: false
          },
          logout: {
            url: "/auth/logout",
            method: "get"
          },
          user: {
            url: "/auth/me",
            method: "get",
            propertyName: false
          }
        },
        tokenRequired: false,
        tokenType: false
      }
    }
  },

  axios: {
    prefix: "/server",
    proxy: true,
    credentials: true
  },

  proxy: {
    "/server": {
      target: "http://localhost:3000/",
      pathRewrite: { "^/server": "" }
    }
  },

  router: {
    middleware: ["auth"]
  },

  serverMiddleware: [
    {
      path: "/server",
      handler: "~/server/server.js"
    }
  ],
  // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
  vuetify: {
    customVariables: ["~/assets/variables.scss"],
    theme: {
      dark: true,
      themes: {
        dark: {
          primary: colors.blue.darken2,
          accent: colors.grey.darken3,
          secondary: colors.amber.darken3,
          info: colors.teal.lighten1,
          warning: colors.amber.base,
          error: colors.deepOrange.accent4,
          success: colors.green.accent3
        }
      }
    }
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {}
};
 

И то server.js , что является точкой входа в мой сервер:

 require("dotenv").config();
const express = require("express");
const app = express();
const passport = require("./loaders/passport.loader")(app);
app.use(express.json());
app.listen(process.env.PORT);
require("./routes/routes")(app, passport);

module.exports = app;
 

И routes.js:

 module.exports = function(app, passport) {
  const userRouter = require("./user.endpoints");
  const authRouter = require("./auth.endpoints")(passport);
  const conceptRouter = require("./concept.endpoints");
  const logger = require("../log/winston.logger");
  const logLevels = require("../log/log.levels.json").levels;
  app.use(function(req, res, next) {
    logger(req.method   " "   req.url, logLevels.http);
    next();
  });
  app.get("/", (req, res, next) => {
    console.log("Here");
    res.send("Here");
  });
  app.use("/auth", authRouter);
  //app.use(isConnected);
  app.use("/api/user", userRouter);
  app.use("/api/concept", conceptRouter);
};
 

And when I’m trying the «/» route, this is what happens :

     npm run dev 

> concept-slide-frontend@1.0.0 dev
> nuxt

ℹ [HPM] Proxy created: /server  -> http://localhost:3000/                                                                                                                                                                                                                                                                                           18:05:49
ℹ [HPM] Proxy rewrite rule created: "^/server" ~> ""                                                                                                                                                                                                                                                                                                18:05:49

   ╭───────────────────────────────────────╮
   │                                       │
   │   Nuxt @ v2.15.4                      │
   │                                       │
   │   ▸ Environment: development          │
   │   ▸ Rendering:   server-side          │
   │   ▸ Target:      server               │
   │                                       │
   │   Listening: http://localhost:3000/   │
   │                                       │
   ╰───────────────────────────────────────╯

ℹ Preparing project for development                                                                                                                                                                                                                                                                                                                 18:05:50
ℹ Initial build may take a while                                                                                                                                                                                                                                                                                                                    18:05:50
ℹ Discovered Components: .nuxt/components/readme.md                                                                                                                                                                                                                                                                                                 18:05:50
✔ Builder initialized                                                                                                                                                                                                                                                                                                                               18:05:50
✔ Nuxt files generated                                                                                                                                                                                                                                                                                                                              18:05:50

✔ Client
  Compiled successfully in 6.01s

✔ Server
  Compiled successfully in 5.29s

ℹ Waiting for file changes                                                                                                                                                                                                                                                                                                                          18:05:57
ℹ Memory usage: 417 MB (RSS: 551 MB)                                                                                                                                                                                                                                                                                                                18:05:57
ℹ Listening on: http://localhost:3000/                                                                                                                                                                                                                                                                                                              18:05:57
2021-04-20T16:06:21.794Z - [HTTP]:      @GET /server OK
2021-04-20T16:06:29.288Z - [HTTP]:      @GET / OK
Here  

                                          
 

As you can see in the logs: the redirection towards the path defined in the config file isn’t working; when I do a GET towards «/server», the console does not print «Here» but when I do a simple GET towards «/», now it’s working.

The redirection is just not applied but there aren’t any errors.