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;