0

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

小米 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 not found 响应,我不明白我配置错了什么。

app.get('/api/products', (req, res) => res.send('products'))
4

1 回答 1

0

你打电话给http://localhost:3000/api/youridhere 了吗?

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

于 2021-05-19T07:12:43.413 回答