const express = require('express') const router = express.Router() const fs = require("fs"); const path = require("path"); const BASE_PATH = __dirname; const STATIC_PATH = `${BASE_PATH}/static`; // Serve static files router.use(express.static(path.join(__dirname, './assets/'))) // Add the required directories router.use((req, res, next) => { const directories = ['/static', '/profiles'] directories.forEach((dir) => { if (!fs.existsSync(BASE_PATH + dir)) { fs.mkdirSync(BASE_PATH + dir) } }) next() }) // Serve Static generated SVGs router.get('/static/:name', async (req, res, next) => { const fileName = req.params.name const filePath = `${STATIC_PATH}/${fileName}` const file = await fs.readFileSync(filePath) res.setHeader('Content-Type', 'image/svg+xml') res.send(file) }) router.use('/api', require('./routes/api').default) router.get('/info', (req, res) => { res.send('Pen-Plotter backend') }) module.exports = router