For a deeper look into our Elektron API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
1 0 0 1

How to setup a timeout for long time waiting response in node js?

I'm trying to set the timeout to prevent continuous running of api call. But it's not working.Here is my code.


const express=require('express'
const app=require('./app')
const http=require('http');
const bodyParser=require('body-parser');
const cookieParser=require('cookie-parser');
const dotenv=require('dotenv');
const ConnectToDb=require('./config/db-connection');
const register=require('./api/routes/auth');
const errorHandler = require('./api/middlewares/errorHandler');

//setting .env file variables
dotenv.config({path:'./.env'})
const PORT=process.env.PORT || 5001;
// ConnectToDb();






const server=http.createServer(app);


ConnectToDb().then(()=>{
    server.listen(PORT,()=>{
        console.log(`Listening on port ${PORT}`);
    })
});

var request=http.request({
    port:PORT,
    host:'192.168.1.247',
    timeout: 5000,
});
request.end();
request.on('timeout', (res)=>{
    console.log("timeout- server taking too long to respond");
  });

Here is the route i'm trying to test.

const express=require('express');
const router=express.Router();
router.get("/",(req,res)=>{
    setTimeout(()=>{
        res.status(400).json({message:'Testing'})
    },10000)

})

module.exports=router;
#technologyrest-apinode-js
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Upvotes
22.1k 59 14 21

Hi @srisrinu1,

This forum is only for LSEG products and services.

For generic programming questions, I would suggest that you raise a query at StackOverflow. Take a look at this similar answer there.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.