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.

Can't use express router for api routes

See original GitHub issue

Environment

  • Operating System: Windows_NT
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-27495976.e31c604
  • Package Manager: yarn@4.0.0-rc.2
  • Builder: vite
  • User Config: meta, components, css, plugins, serverMiddleware, modules, buildModules, typescript, windicss
  • Runtime Modules: -
  • Build Modules: nuxt-windicss@2.2.10, @vueuse/nuxt@8.2.5, @pinia/nuxt@0.1.8

Reproduction

I wanted to build serverless api using express with these config

  • nuxt.config.ts
serverMiddleware: [{ path: '/api', handler: '~/server/api' }]
  • server/api/index.ts
import express from 'express'
import authRouter from './routes/v1/auth'

const app = express()
app.use(express.json())

app.use('/v1/auth', authRouter)

app.use((req, res) => {
  res.status(200).json({ Status: 'API OK', created: Date.now().toString(), version: '1.0.0' })
})

export default app
  • server/api/routes/auth.ts
import { Router } from 'express'

import stellar from '../../objects/stellar'

const authRouter = Router()

authRouter.get('/login', (req, res) => { return res.redirect(stellar.getLoginUrl()) })

export default authRouter

Describe the bug

When I try to access /api/v1/auth/login I got the response

{
"url": "/api/v1/auth/login",
"statusCode": 404,
"statusMessage": "Page not found: /api/v1/auth/login",
"message": "Page not found: /api/v1/auth/login"
}

but with only /api I got

{
"Status": "API OK",
"created": "1649827202155",
"version": "1.0.0"
}

Additional context

No response

Logs

[Vue Router warn]: No match found for location with path "/api/v1/auth/login"

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Aareksiocommented, Apr 24, 2022

@Sgadmer @Gusb3ll https://stackblitz.com/edit/github-5bzsuf?file=server%2Fapi.ts

Full example with express, works well once set up properly. Avoid server/api and server/middleware paths as they are automatically imported by nuxt. I’d recommend something akin to server/app.

I am not sure how compatible is h3’s req/res with express, but seems to work with the basic example derived from OP’s code.

0reactions
Gusb3llcommented, Apr 25, 2022

Holy Moly, its working) Thank you so much! Actual solution was in this lines:

    { path: '/api', handler: '~/server/api.ts' }, // `/api/**` does NOT handle /api
    { path: '/api/**', handler: '~/server/api.ts' },

I washt use second (/**) prop, because in earlier version of nuxt (3.0.0-27488206.1ffcc74), it was just fine to send only first one and everything works well

Ah I see, I didn’t use the /** prop too as it works for the prev version of nuxt3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot GET /foo when using express.Router() - Stack Overflow
I'm trying to figure out why the routing doesn't work if I use expressjs , I ...
Read more >
FAQ - Express.js
Add routes dynamically at runtime on an instance of express.Router() so the routes are not superseded by a middleware function. How do I...
Read more >
Express.js router error 'Cannot GET /something'
I was trying to write a web API using Express.js routers, when I encountered ... routes/usersRoute'); var app = express(); app.use('/users', ...
Read more >
Building a Node.JS REST API With Express - Techievor
Response from our first route! Now we need to think about the functionality we want to provide our end users. If we allow...
Read more >
How to Handle Errors in an Express and Node.js App
Put this code after the route declaration in index.js before app.listen and after the first app.use , and restart the server and then...
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