Skip to content
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

Nextjs is not supported as a server #4499

Closed
shihuojian opened this issue Apr 17, 2024 · 2 comments
Closed

Nextjs is not supported as a server #4499

shihuojian opened this issue Apr 17, 2024 · 2 comments
Labels
support Questions, discussions, and general support

Comments

@shihuojian
Copy link

Runtime

node

Runtime version

21.3.2

Module version

21.3.2

Used with

No response

Any other relevant information

No response

How can we help?

const Hapi = require('@hapi/hapi');
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
       
    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    server.route({
        method: ["POST","GET"],
        path: '/api/test',
        handler: async (request, h) => {
            const {req,res} = h.request.raw;
            await handle(req,res);
            return h.close;
        }
    });


    server.start();
    console.log('> Ready on http://localhost:3000');

}).catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
})

As above, GET can be successful, but the post request has been unresponsive. How to solve the problem? I tried experss and the post request was successful.

@shihuojian shihuojian added the support Questions, discussions, and general support label Apr 17, 2024
@devinivy
Copy link
Member

You'll want to ensure hapi doesn't consume the request payload in that case. Give something like this a whirl:

    server.route({
        method: ["POST","GET"],
        path: '/api/test',
        options: {
            payload: {
                parse: false,
                output: 'stream'
            }
        },
        handler: async (request, h) => {
            const {req,res} = h.request.raw;
            try {
                await handle(req,res);
            } catch (err) {
                // handle err however you like, perhaps by logging
                return h.close
            }
            return h.abandon;
        }
    });

@shihuojian
Copy link
Author

在这种情况下,您需要确保 hapi 不会消耗请求负载。尝试一下这样的事情:

    server.route({
        method: ["POST","GET"],
        path: '/api/test',
        options: {
            payload: {
                parse: false,
                output: 'stream'
            }
        },
        handler: async (request, h) => {
            const {req,res} = h.request.raw;
            try {
                await handle(req,res);
            } catch (err) {
                // handle err however you like, perhaps by logging
                return h.close
            }
            return h.abandon;
        }
    });

Thank you very much, this is very helpful to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

2 participants