-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to run with next.js? #1122
Comments
Hey @xiaoyu-tamu , I'am not an expert but what i have seen is that you have to catch the request before handle it like the following code: server.get('*', (req, res) => {
handle(req, res)
}) |
@adrien2p Thanks for the help. I also tried this solution but it's not working as expected. |
You also get a 404 ? |
I use nextjs and nestjs too, my workaround is to run both server separately and proxy the request from nextjs server to nestjs controller //server.js(nextjs render server)
const port = parseInt(process.env.PORT, 10) || 3000
const env = process.env.NODE_ENV
const dev = env !== 'production'
const app = next({
dir: '.',
dev
})
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const server = express()
server.use(cookieParser());
// ONLY DEV PURPOSE
const devProxy = {
'/your entry': {
target: 'YOUR AWESOME API',
changeOrigin: true
}
}
if (devProxy) {
const proxyMiddleware = require('http-proxy-middleware')
Object.keys(devProxy).forEach(function (context) {
server.use(proxyMiddleware(context, devProxy[context]))
})
}
server.all('*', (req, res) => handle(req, res))
server.listen(port, err => {
if (err) {
throw err
}
console.log(`> Ready on port ${port} [${env}]`)
})
})
.catch(err => {
console.log('An error occurred, unable to start the server')
console.log(err)
}) |
I've made a pretty basic example showing the two working together, I haven't written very much documentation around it, but the majority of the code is in the main.ts file and then the render module. https://github.com/kyle-mccarthy/nest-next-starter Right now it works with the latest next and nest versions. I will try to update the documentation in the next could days to explain more about what is going on. In short, I bind a global middleware and filter provided by the render module. The middleware gets the |
This works for successful responses. But if any nestjs controller throws an error, next always shows a 404. Do you know of a way to fix this? |
I ended up releasing a package called nest-next to make the integration easier to setup. |
I create a nestjs modlue named nest-next-module, welcome to using it. ;D |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
All requests are handled by next.js (SSR Framewrok) . I get a 404 error throw by next.js. when i go to localhost:3000/test. But this url is watched by my AppController, it should return the string hello world.
Environment
The text was updated successfully, but these errors were encountered: