Update server.js
Browse files
server.js
CHANGED
|
@@ -10,6 +10,12 @@ console.log('Environment loaded, PORT:', process.env.PORT);
|
|
| 10 |
const app = express();
|
| 11 |
app.use(express.json());
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
// Handle JSON parsing errors
|
| 14 |
app.use((err, req, res, next) => {
|
| 15 |
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
|
|
@@ -90,6 +96,57 @@ app.post("/api/market-research", (req, res) => {
|
|
| 90 |
res.json(mockResponse);
|
| 91 |
});
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
// Error handling middleware
|
| 94 |
app.use((err, req, res, next) => {
|
| 95 |
console.error('Server error:', err.message);
|
|
@@ -114,11 +171,12 @@ process.on('unhandledRejection', (reason, promise) => {
|
|
| 114 |
console.error('Reason:', reason);
|
| 115 |
});
|
| 116 |
|
| 117 |
-
try {
|
| 118 |
app.listen(PORT, "0.0.0.0", () => {
|
| 119 |
console.log(`β
Server successfully running on port ${PORT}`);
|
| 120 |
console.log(`β
Health endpoint: GET /health`);
|
| 121 |
console.log(`β
API endpoint: POST /api/market-research`);
|
|
|
|
| 122 |
});
|
| 123 |
} catch (error) {
|
| 124 |
console.error('β Failed to start server:', error.message);
|
|
|
|
| 10 |
const app = express();
|
| 11 |
app.use(express.json());
|
| 12 |
|
| 13 |
+
// Log all incoming requests for debugging
|
| 14 |
+
app.use((req, res, next) => {
|
| 15 |
+
console.log(`${req.method} ${req.path} - Headers:`, req.headers);
|
| 16 |
+
next();
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
// Handle JSON parsing errors
|
| 20 |
app.use((err, req, res, next) => {
|
| 21 |
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
|
|
|
|
| 96 |
res.json(mockResponse);
|
| 97 |
});
|
| 98 |
|
| 99 |
+
// Add /run endpoint for n8n compatibility (original workflow uses this)
|
| 100 |
+
app.post("/run", (req, res) => {
|
| 101 |
+
console.log('Received request at /run endpoint');
|
| 102 |
+
|
| 103 |
+
const { keyword, api_key, task = "market_research" } = req.body || {};
|
| 104 |
+
|
| 105 |
+
if (!keyword) {
|
| 106 |
+
return res.status(400).json({ error: "keyword is required" });
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
if (!api_key) {
|
| 110 |
+
return res.status(400).json({
|
| 111 |
+
error: "API key required",
|
| 112 |
+
message: "Send api_key in request body from n8n credentials"
|
| 113 |
+
});
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
console.log('Processing /run request for task:', task, 'keyword:', keyword);
|
| 117 |
+
|
| 118 |
+
// Return mock response in the format n8n expects
|
| 119 |
+
const mockResponse = {
|
| 120 |
+
marketTitle: `Global ${keyword} Market Analysis`,
|
| 121 |
+
marketOverview: {
|
| 122 |
+
pastYear_2023: 10.5,
|
| 123 |
+
currentYear_2025: 15.2,
|
| 124 |
+
forecastYear_2033: 28.7,
|
| 125 |
+
global_cagr_Forecast: 12.5
|
| 126 |
+
},
|
| 127 |
+
marketSegments: [
|
| 128 |
+
{
|
| 129 |
+
segment: "Primary Segment",
|
| 130 |
+
segment_marketShare_2023: "35%",
|
| 131 |
+
segment_marketShare_2025: "38%",
|
| 132 |
+
segment_marketShare_2033: "42%",
|
| 133 |
+
segment_cagr: "12%"
|
| 134 |
+
}
|
| 135 |
+
],
|
| 136 |
+
competitiveLandscape: [
|
| 137 |
+
{
|
| 138 |
+
company: "Leading Company",
|
| 139 |
+
player_marketShare_2025: 25
|
| 140 |
+
}
|
| 141 |
+
],
|
| 142 |
+
status: "success",
|
| 143 |
+
message: "Mock data from /run endpoint"
|
| 144 |
+
};
|
| 145 |
+
|
| 146 |
+
console.log('Returning mock response from /run');
|
| 147 |
+
res.json(mockResponse);
|
| 148 |
+
});
|
| 149 |
+
|
| 150 |
// Error handling middleware
|
| 151 |
app.use((err, req, res, next) => {
|
| 152 |
console.error('Server error:', err.message);
|
|
|
|
| 171 |
console.error('Reason:', reason);
|
| 172 |
});
|
| 173 |
|
| 174 |
+
try {
|
| 175 |
app.listen(PORT, "0.0.0.0", () => {
|
| 176 |
console.log(`β
Server successfully running on port ${PORT}`);
|
| 177 |
console.log(`β
Health endpoint: GET /health`);
|
| 178 |
console.log(`β
API endpoint: POST /api/market-research`);
|
| 179 |
+
console.log(`β
n8n endpoint: POST /run (with X-OpenClaw-Key header)`);
|
| 180 |
});
|
| 181 |
} catch (error) {
|
| 182 |
console.error('β Failed to start server:', error.message);
|