I am working on a project where I have a particular requirement
- Working on the client side of the request, I want to terminate the tls connection all together incase a request timesout (and not crash the node server)
- Else, proceed normally with the response
I have written the following code which works fine for timeout cases (it is a hacky one though) where I am just crashing the node server whenever a timeout happens and then automatically restarting it using PM2. Pasting the snippet below.
Requirement I tried to dig deeper to figure out how we can manually close a tls connection in express but was unsuccesful (hence the temporary hack). Any help from the community would be really appreciated
const express = require('express')
const axios = require('axios').default;
const https = require('https');
const fs = require('fs');
const app = express()
const port = 3000
const httpsAgent = new https.Agent({
rejectUnauthorized: false, // (NOTE: this will disable client verification)
cert: fs.readFileSync("certificate.crt"),
key: fs.readFileSync("server.key"),
ca: fs.readFileSync("ca.crt"),
})
const url = "--"
app.use(express.json())
app.all('/proxy', (req, res) => {
axios.post(url, req.body, { httpsAgent, headers: req.headers, timeout: 10 * 1000 }).then((ds) => {
res.send(ds.data)
}).catch(() => {
process.exit()
res.connection.end()
})
})
app.listen(port, () => {
console.log("STARTED LISTENING ON PORT: " + port.toString())
})