danielzzz / node-ping

a poor man's ping library (using udp scanning) for node
MIT License
336 stars 105 forks source link

Using node-ping in REST API (ExpressJS) #153

Closed baur closed 2 years ago

baur commented 2 years ago

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});
mondwan commented 2 years ago

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: @.***>

baur commented 2 years ago

return res.json not helps me

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

mondwan commented 2 years ago

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 commented 2 years ago

Please reopen it if you have any new updates.