pencil_automator/jsonUploader.js
2026-03-06 17:42:19 +00:00

51 lines
No EOL
1.5 KiB
JavaScript

const express = require('express');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 3000; // Or whichever port your server uses
app.use(cors());
app.use(express.json({ limit: '10mb' }));
// 1. Map the specific URL path to a local folder (e.g., 'pencil_data')
app.use('/Pencil_Automator', express.static(path.join(__dirname, 'pencil_data')));
app.post('/upload-json', (req, res) => {
const jsonData = req.body;
if (!jsonData) {
return res.status(400).json({ error: 'No JSON data provided' });
}
// 2. Hardcode the file name
const fileName = 'PA-keys.json';
const dirPath = path.join(__dirname, 'pencil_data');
const filePath = path.join(dirPath, fileName);
// Ensure the local directory exists
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
// 3. Write (and overwrite) the JSON file
fs.writeFile(filePath, JSON.stringify(jsonData, null, 2), (err) => {
if (err) {
console.error('Error writing file:', err);
return res.status(500).json({ error: 'Failed to save file' });
}
// Return the exact public URL you requested
const publicUrl = `https://ai-sandbox.oliver.solutions/Pencil_Automator/${fileName}`;
res.status(200).json({
message: 'File updated successfully',
url: publicUrl
});
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});