Can't use express router for api routes
See original GitHub issueEnvironment
- 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:
- Created a year ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Sgadmer @Gusb3ll https://stackblitz.com/edit/github-5bzsuf?file=server%2Fapi.ts
Full example with
express
, works well once set up properly. Avoidserver/api
andserver/middleware
paths as they are automatically imported by nuxt. I’d recommend something akin toserver/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.
Ah I see, I didn’t use the
/**
prop too as it works for the prev version of nuxt3.