为什么我无法在nuxt中的serverMiddleware中创建带有参数的路由?

问题描述 投票:0回答:3

我在配置 serverMIddleware 属性时遇到问题,一切正常,但我无法创建除初始路由之外的任何其他路由

Mi nuxt.config.js 文件:

  export default {
      // Target: https://go.nuxtjs.dev/config-target
      target: 'static',
      ssr: true,
      // Global page headers: https://go.nuxtjs.dev/config-head,
      modules: [
        '@nuxtjs/axios',
        '@nuxtjs/auth-next',
        // '~/modules/api/index.js'
      ],
      serverMiddleware:[
        { path: '/api', handler: '~/api/index.js'},
      ],
      axios: {
        baseURL: 'http://localhost:3000/',
      },
      head: {
        title: 'Encontrar Hogar',
        htmlAttrs: {
          lang: 'en'
        },
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          { hid: 'description', name: 'description', content: 'Encontra tu nuevo hogar en chacabuco. Todas las propiedades e inmuebles de Chacabuco en un mismo lugar' },
        ],
        link: [
          { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
          { rel: 'stylesheet', href: '/normalize.css' },
          { rel: 'stylesheet', href: "https://unpkg.com/swiper/swiper-bundle.min.css" }
        ],
        script: [
          { src: "https://kit.fontawesome.com/57d52ad1ee.js", crossorigin: "anonymous" },
        ]
      },
      loading: {
        color: 'black',
        height: '8px',
        continuous: true
      },
      css: [
        '@/assets/css/index.css'
      ],
      auth: {
        cookie:{
          options:{
            secure: true
          }
        },
        strategies: {
          local: {
            token: {
              property: 'token'
            },
            user: {
              property: 'user'
            },
            endpoints: {
              login: { url: '/api/usuarios/login', method: 'post', propertyName: 'data' },
              user: { url: '/api/usuarios/mi-perfil', method: 'get', propertyName: 'data' },
              logout: false
            }
          }
        }
      },
      // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
      plugins: ['~/plugins/swiper.js'],
      // Auto import components: https://go.nuxtjs.dev/config-components
      components: true
    }

/api/index.js 文件:

const bodyParser = require('body-parser')
const app = require('express')()

app.get('/api/:id?', (req, res) => res.send('hello'))

module.exports = app

如果我尝试创建像下面将要留下的那样的端点,则通过浏览器或邮递员发出 get 请求时得到的唯一结果是 404 未找到响应,我不明白我配置了什么错误。

app.get('/api/products', (req, res) => res.send('products'))
express nuxt.js middleware nuxt.config.js
3个回答
0
投票

您是否致电 http://localhost:3000/api/youridhere

根据您的配置和serveMiddleware,您的链接应该是http://localhost:3000/api/api/youridhere


0
投票

您有静态目标,如果您希望服务器中间件工作,请更改为“服务器”


0
投票

您可以将

regex
用作
path
,如下所示:

{
  path: /^\/api\/\d$/i,
  handler: '~/api/index.js'
}
© www.soinside.com 2019 - 2024. All rights reserved.