-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
30 lines (26 loc) · 914 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require('dotenv').config();
const path = require('path');
const express = require('express');
const fs = require('fs');
const app = express();
const port = 3000;
app.get('/code/:filename', (req, res) => {
const filename = req.params.filename;
fs.readFile(path.join(__dirname, '/code/', filename), 'utf8', (err, data) => {
if (err) {
console.error(err);
return res.status(500).send(`An error occurred while reading ${filename}`);
}
const replacedData = data.replace('MAPBOX_API', process.env.MAPBOX_API);
res.setHeader('Content-Type', 'application/javascript');
res.send(replacedData);
});
});
app.use(express.static(path.join(__dirname, '/')));
// Catch-all route handler
app.get('*', (req, res) => {
res.status(404).send(`Cannot find ${req.originalUrl} on this server!`);
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});