Files
ical-example/index.js

57 lines
1.6 KiB
JavaScript

const express = require("express");
const fs = require("fs");
const ics = require("ics");
const marked = require("marked");
// Define defaults of HTTP port/host, can be redefined by environment variables.
const HTTP_PORT = process.env.HTTP_PORT || 8080;
const HTTP_HOST = process.env.HTTP_HOST || "localhost";
const README_FILE = "README.md";
// Init the Express application instance.
const app = express();
// Just show `README.md` file as a main page.
app.get("/", (req, res) => {
fs.readFile(README_FILE, { encoding: "utf8" }, (error, file) => {
if (error) {
console.error(error);
process.exit(1);
return;
}
const html = marked.marked(file);
res.send(html);
});
});
// Generate ICS-file example tuned with URL query parameters.
app.get("/generate-event", function (req, res) {
// Load event template from the file.
const eventTemplate = require("./eventTemplate");
let event = JSON.parse(JSON.stringify(eventTemplate.eventTemplate));
event.description = req.query.description;
ics.createEvent(event, (error, iCalEventText) => {
if (error) {
console.error(error);
return;
}
// Response as a file if required.
if (req.query.output === "file") {
res.set({
"Content-Type": "text/calendar",
"Content-Disposition": "inline;filename=calendar.ics",
});
} else {
res.set({
"Content-Type": "text/plain",
});
}
res.send(iCalEventText);
});
});
// Starting a HTTP server.
console.log(`Server is running on http://${HTTP_HOST}:${HTTP_PORT}...`);
app.listen(HTTP_PORT);