Why can't I create routes with parameters in my serverMiddleware in nuxt?

413 Views Asked by At

I'm having a problem configuring the serverMIddleware property, everything works perfectly, but I can't create any other route than the initial route

Mi nuxt.config.js file:

  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 file:

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

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

module.exports = app

If I try to create endpoints like the one that I will leave below, the only thing I get when making the get requests through the browser or postman is a 404 not found response, I do not understand what I am configuring wrong.

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

There are 3 best solutions below

0
On

you can use regex for path like this:

{
  path: /^\/api\/\d$/i,
  handler: '~/api/index.js'
}
0
On

Did you call http://localhost:3000/api/youridhere ?

According to your configuration and serveMiddleware, your link should be http://localhost:3000/api/api/youridhere

0
On

You have static target, change to "server" if you want serverMiddleware to work