I have set up an Express server to handle webhook requests from TradingView, but the requests don’t seem to be reaching my server. Below is my server code and the steps I’ve taken to debug the issue. Despite configuring everything according to the documentation, the server logs do not show any incoming requests when an alert is triggered in TradingView.
Environment Variables (.env file):
PORT="8081"
DATABASE_USERNAME="root"
DATABASE_PASSWORD="rootpassword"
DATABASE_HOST="localhost"
DATABASE_PORT=3306
DATABASE_NAME="testtest"
Server Code:
import express from "express";
import Knex from "knex";
import bodyParser from "body-parser";
import axios from "axios";
import dotenv from 'dotenv';
import cors from 'cors';
import { SHA3 } from 'sha3';
const validSide = ["buy", "sell"];
const validPosition = ["long", "short"];
function hash(text) {
const hasher = new SHA3(512);
hasher.update(text);
return hasher.digest('hex');
}
dotenv.config();
const app = express();
app.use(bodyParser.json());
app.use(cors());
const knex = Knex({
client: 'mysql2',
connection: {
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
port: process.env.DATABASE_PORT
}
});
app.use((req, res, next) => {
console.log('Request Method:', req.method);
console.log('Request URL:', req.url);
console.log('Request Headers:', req.headers);
if (req.method === 'POST' || req.method === 'PUT') {
console.log('Request Body:', req.body);
}
next();
});
app.get('/proxy', (req, res) => {
res.send('Trade Proxy');
});
app.post('/proxy/response', async (req, res) => {
const body = req.body;
console.log('Received webhook:', body);
if (!body.symbol || !body.side || !validSide.includes(body.side) || !body.position || !validPosition.includes(body.position) || !body.strategy || !body.hash || !body.close) {
return res.status(400).json({
success: false,
message: '400 REQUEST ERROR',
data: null
});
}
try {
const [strategy] = await knex('strategy_entity')
.select("*")
.where({ uuid: body.strategy });
if (!strategy || strategy.hash !== body.hash) {
return res.status(404).json({
success: false,
message: "Not FOUND STRATEGY",
data: null
});
}
const rows = await knex('server_entity')
.select("*")
.where({
strategy_uuid: body.strategy,
status: true
});
for (const entity of rows) {
try {
const serverURL = Buffer.from(entity.server, 'base64').toString('utf8');
const requestData = {
symbol: body.symbol,
strategy: entity.uuid,
side: body.side,
position: body.position,
close: body.close
};
const response = await axios.post(serverURL, requestData, {
headers: { 'Content-Type': 'application/json' }
});
console.log(`Response from ${serverURL}: ${response.status}`);
} catch (error) {
console.log(`Error sending POST request: ${error.message}`);
}
}
return res.status(200).json({
success: true,
message: null,
data: null
});
} catch (err) {
console.log(err);
return res.status(500).json({
success: false,
message: 'Database Query Error',
data: null
});
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log("nn");
console.log("┌───────────────────────────────────────────────────┐");
console.log("│ Express │");
console.log(`│ http://127.0.0.1:${port} │`);
console.log(`│ (bound on host 0.0.0.0 and port ${port}) │`);
console.log("│ │");
console.log("└───────────────────────────────────────────────────┘");
});
Steps I’ve Taken to Debug:
Verified Server is Running: Confirmed that the server is running and accessible via http://localhost:8081/proxy.
Checked Webhook URL: Set the webhook URL in TradingView to http://my-server-ip:8081/proxy/response.
Checked Firewall and Network Settings: Ensured that the server can receive external requests by checking firewall and network settings.
Log Requests: Added middleware to log all incoming requests. However, no requests are logged when the TradingView alert is triggered.
When I tested the webhook functionality by registering it with TradingView using Discord’s webhook feature, it worked perfectly. However, when sending the webhook to my server, even with middleware set up for logging, I noticed that no requests were received.