question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuxt blocks Fastify routes

See original GitHub issue

It appears that Nuxt nuxt.render method blocks Fastify routes.

Reproduction

Use npx create-nuxt-app <project-name> with the following configuration

create-nuxt-app v2.15.0
✨  Generating Nuxt.js project in Test
? Project name Test
? Project description My awe-inspiring Nuxt.js project
? Author name 
? Choose programming language JavaScript
? Choose the package manager Yarn
? Choose UI framework Vuetify.js
? Choose custom server framework Fastify
? Choose Nuxt.js modules Axios, Progressive Web App (PWA) Support, DotEnv
? Choose linting tools ESLint, Prettier
? Choose test framework AVA
? Choose rendering mode Universal (SSR)
? Choose development tools jsconfig.json (Recommended for VS Code)
yarn run v1.22.4

Then add a simple fastify route to /server/index.js

fastify.get('/test', (request, reply) => {
  reply.send({ hello: 'world' })
})

Result

Fastify routes returns 404

What i have tried

  • Disabling Nuxt render (fastify.use(nuxt.render)) does allow Fastify routes to be exposed

I guess adding a whitelist for Fastify routes and handling that with Nuxt.render would work, but that would be inconvenient.

Thank you

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kdeenanauthcommented, May 8, 2020

Yeah I noticed the same thing. I don’t think nuxt’s render method is calling next.

For anyone else, I followed the whitelist approach as noted:

fastify.use(
    (_req, res, next) => {
      if (_req.url.startsWith("/api") > 0) {
        next()
      } else {
        nuxt.render(_req, res, next)
      }
    }
  )
0reactions
pi0commented, Jun 3, 2020

@paulogr Unfortunately it is not universally possible because this means we need to load/init the entire SSR bundle even for client-side calls to functions. But the good news is that functions supporting a similar feature called services so we can have axios service.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create api routes with Fastify and Nuxt? - Stack Overflow
js process with a non-zero exit code. How can I add api routes using Fastify and Nuxt? I want to avoid creating a...
Read more >
Plugins - Fastify
Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator, or whatever....
Read more >
The Ultimate Nuxt API Setup
First we need to use a plugin to inject $api everywhere. If the request is running entirely on the server (SSR), we assign...
Read more >
How to build a blazingly fast API with Fastify - LogRocket Blog
5 important Fastify plugins you should know · 1. Installation and requirements · 2. Create server.js · 3. Adding CRUD routes · 4....
Read more >
Build a CRUD API with Fastify - DEV Community ‍ ‍
The next thing we would do is to assign a port to a PORT variable, ... Now let's create a simple route for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found