|
| 1 | +# Tile server |
| 2 | + |
| 3 | +Set up tile-server to serve the tiles locally |
| 4 | + |
| 5 | +Make a server.cjs file that looks like this: |
| 6 | + |
| 7 | +```javascript |
| 8 | +const express = require("express"); |
| 9 | +const path = require("path"); |
| 10 | +const fs = require("fs"); |
| 11 | +const mime = require("mime-types"); |
| 12 | +const cors = require("cors"); |
| 13 | + |
| 14 | +const app = express(); |
| 15 | +const PORT = 8080; |
| 16 | +const TILE_DIR = path.join(__dirname, "tiles-with-imd-data"); // your z/x/y.pbf folder |
| 17 | + |
| 18 | +app.use(cors()); // Enables CORS for all routes |
| 19 | + |
| 20 | +// Set correct headers for .pbf files |
| 21 | +app.get("/:z/:x/:y.pbf", (req, res) => { |
| 22 | + const { z, x, y } = req.params; |
| 23 | + const tilePath = path.join(TILE_DIR, z, x, `${y}.pbf`); |
| 24 | + |
| 25 | + if (!fs.existsSync(tilePath)) { |
| 26 | + return res.status(404).send("Tile not found"); |
| 27 | + } |
| 28 | + res.setHeader("Access-Control-Allow-Origin", "*"); |
| 29 | + res.setHeader("Content-Type", "application/x-protobuf"); |
| 30 | + res.setHeader("Content-Encoding", "gzip"); |
| 31 | + |
| 32 | + fs.createReadStream(tilePath).pipe(res); |
| 33 | +}); |
| 34 | + |
| 35 | +// Optional: serve an index or static frontend here |
| 36 | + |
| 37 | +app.listen(PORT, () => { |
| 38 | + console.log(`Tile server running at http://localhost:${PORT}/`); |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +Then from the terminal run: |
| 43 | + |
| 44 | +```console |
| 45 | +node server.cjs |
| 46 | +``` |
0 commit comments