node-ping icon indicating copy to clipboard operation
node-ping copied to clipboard

Using node-ping in REST API (ExpressJS)

Open baur opened this issue 3 years ago • 3 comments

Hi, I get an error if use this package in the ExpressJS route. but I see result on console console.log(pingResult); of course, error is not related with ping package, but I need help ...

router.get('/camera/ping', asyncHandler(async (req, res) => {
    try {
            const ip="10.x.x.x";
            let pingResult;
            try {
                pingResult = await ping.promise.probe(ip, {
                    timeout: 5,
                    min_reply: 4
                });
                console.log(pingResult);
            } catch (error) {
                pingResult = error;
            };
            res.json({pingResult});
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

code works without ping

router.get('/camera/ping', asyncHandler(async (req, res) => {
    try {
            let pingResult;
            // try {
            //     pingResult = await ping.promise.probe(ip, {
            //         timeout: 5,
            //         min_reply: 4
            //     });
            // } catch (error) {
            //     pingResult = error;
            // };
            res.json({pingResult});

baur avatar Jul 28 '22 10:07 baur

Try return res.json.

I am not really familiar with latest JS async syntax. But, according to the error message, I guess it is something about you have not chain the async code properly. Try to debug in this direction

On Thu, 28 Jul 2022, 11:16 Bauyrzhan Yessetov, @.***> wrote:

Hi, I get an error if use this package in the ExpressJS route. but I see result on console console.log(pingResult); of course, error is not related with ping package, but I need help ...

router.get('/camera/ping', asyncHandler(async (req, res) => { try { const ip="10.x.x.x"; let pingResult; try { pingResult = await ping.promise.probe(ip, { timeout: 5, min_reply: 4 }); console.log(pingResult); } catch (error) { pingResult = error; }; res.json({pingResult});

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

code works without ping

router.get('/camera/ping', asyncHandler(async (req, res) => { try { const ip="10.x.x.x"; res.json({ip:ip});

— Reply to this email directly, view it on GitHub https://github.com/danielzzz/node-ping/issues/153, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA5YBI757NUQGJ4HXSOPV5TVWJMYPANCNFSM544WVGNA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mondwan avatar Aug 04 '22 14:08 mondwan

return res.json not helps me

I'm using asyncHandler, I think there is no problem with async ...

baur avatar Aug 08 '22 14:08 baur

Try to log whether there are any errors on your exception

On Mon, 8 Aug 2022, 15:43 Bauyrzhan Yessetov, @.***> wrote:

return res.json not helps me

I'm using asyncHandler, I think there is no problem with async ...

— Reply to this email directly, view it on GitHub https://github.com/danielzzz/node-ping/issues/153#issuecomment-1208223192, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA5YBI6ZMTXHTRQ7UD63XHLVYEMKRANCNFSM544WVGNA . You are receiving this because you commented.Message ID: @.***>

mondwan avatar Aug 08 '22 15:08 mondwan

Please reopen it if you have any new updates.

mondwan avatar Nov 24 '22 11:11 mondwan