Skip to content

Commit

Permalink
tile map server added
Browse files Browse the repository at this point in the history
  • Loading branch information
itzmeanjan committed Apr 12, 2019
1 parent dd21924 commit afb3a2b
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tms/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// this is a simple tile map server implementation, built with Express, serves tiles indentified by combination of zoom_level, row_id and column_id
const express = require('express');
const app = express(); // express app's instance gets created here
const join = require('path').join;
const exists = require('fs').exists; // only importing `exists` method
let tile_path = '/home/anjan/Documents/my_programs/still_working/python_codes/mapZ/tiles/';
app.get('/tile/:zoom/:row/:col.png', (req, res) => {
// this is the path we're listening to
// where zoom, request param, gives us zoom level for that tile requested
// row, gives row number of tile to be served
// col, gives column number of tile to be served
console.log(`\t[+]Incoming request from --- ${req.ip}`);
var target_path = join(tile_path, `${req.params.zoom
}_${req.params.row}_${req.params.col}.png`); // this is requested tile
exists(target_path, (e) => {
if (e) {
res.status(200).type('png').sendFile(target_path, {
headers: {
'x-timestamp': Date.now(),
'x-sent': true
},
dotfiles: 'deny' // setting header
}, (err) => {
if (err)
console.log(`\t\t[!]${err}`); // denotes error
});
// sets status code
// sets Content-Type field in Header
// sends tile
}
else {
res.status(404).json({ err: 'Illegal tile requested' }); // if requested tile is not available
}
});
});
app.listen(8000, '0.0.0.0', () => {
// tms listens at 0.0.0.0:8000, so that it can be accessed via both localhost and devices present in local network
console.log('[+]Tile Map Server listening at - `0.0.0.0: 8000`\n');
});
Loading

0 comments on commit afb3a2b

Please sign in to comment.