-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
44 lines (41 loc) · 1.54 KB
/
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const express = require('express');
const tmx = require('tmx-parser');
const app = express();
app.listen(8000, () => {
console.log("App started.");
});
app.use(express.static('public'));
app.get('/maps/:mapname', (req, res) => {
console.log(req.params.mapname);
const filename = './maps/'+req.params.mapname+'.tmx';
tmx.parseFile(filename, (err, map)=> {
if (err) {
console.log(err);
res.redirect('/');
return;
}
console.log("converting map");
// Code from https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json
// Note: cache should not be re-used by repeated calls to JSON.stringify.
var cache = [];
const jase = JSON.stringify(map, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Duplicate reference found
try {
// If this value does not reference a parent it can be deduped
return JSON.parse(JSON.stringify(value));
} catch (error) {
// discard key if value cannot be deduped
return;
}
}
// Store value in our collection
cache.push(value);
}
return value;
});
console.log("map converted");
res.send(JSON.stringify(jase));
})
})