Files

46 lines
1.9 KiB
JavaScript

// Tiny static server for the minimal playground add-in (HTTPS for Office add-in compatibility)
require('dotenv').config();
const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');
const app = express();
const port = process.env.ADDIN_PORT || 3003;
app.use(express.static(path.join(__dirname, 'public')));
app.get('/help.html', (req,res)=> res.send('<h2>Minimal Playground Add-in</h2><p>Sideload manifest to Excel, then open the Add-ins pane and click "Managers UI".</p>'));
// Use office-addin-dev-certs (installed via npx office-addin-dev-certs install)
const userHome = process.env.USERPROFILE || process.env.HOME;
const certDir = path.join(userHome, '.office-addin-dev-certs');
const certPath = path.join(certDir, 'localhost.crt');
const keyPath = path.join(certDir, 'localhost.key');
let server;
if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {
const httpsOptions = {
cert: fs.readFileSync(certPath),
key: fs.readFileSync(keyPath)
};
server = https.createServer(httpsOptions, app);
server.listen(port, '0.0.0.0', () => {
console.log(`minimal-playground add-in server (HTTPS) listening on https://localhost:${port}`);
});
} else {
console.log('No office-addin-dev-certs found. Run: npx office-addin-dev-certs install');
server = app.listen(port, '0.0.0.0', () => {
console.log(`minimal-playground add-in server (HTTP) listening on http://localhost:${port}`);
});
}
server.on('error', (err) => {
console.error('Server error:', err);
});
// Only handle explicit shutdown signals
process.on('SIGINT', () => { console.log('Received SIGINT, shutting down...'); server.close(); process.exit(0); });
process.on('SIGTERM', () => { console.log('Received SIGTERM, shutting down...'); server.close(); process.exit(0); });
// Prevent the process from exiting on its own
process.stdin.resume();