Files
org-todo-pwa/server.js

54 lines
1.3 KiB
JavaScript

const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3044;
app.use(bodyParser.json());
app.use(express.static('public'));
// Ensure the /data directory exists
const dataDir = path.join('/data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
// Endpoint to receive task data and append to file
app.post('/add-task', (req, res) => {
const { subject, description, scheduled } = req.body;
const currentDateTime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
let orgFormattedData = `
* TODO ${subject}
SCHEDULED: <${scheduled}>
:LOGBOOK:
- State "TODO" from "TODO" [${currentDateTime}]
:END:
`;
if (description) {
orgFormattedData = `
* TODO ${subject}
${description}
SCHEDULED: <${scheduled}>
:LOGBOOK:
- State "TODO" from "TODO" [${currentDateTime}]
:END:
`;
}
const filePath = path.join(dataDir, 'tasks.org');
fs.appendFile(filePath, orgFormattedData, (err) => {
if (err) {
return res.status(500).send('Error writing to file.');
}
res.send({ message: 'Task added successfully!' });
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});