file: add files to server directory
This commit is contained in:
1 parent
eaf1d6eb8d
commit
561f4899bd
14 files changed
+3310
No files matched your search
@@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const axios = require("axios");
|
||||
const url = require("url");
|
||||
|
||||
router.get("/:query", async (req, res) => {
|
||||
try {
|
||||
// Add API key & query strings
|
||||
const params = new URLSearchParams({
|
||||
access_token: process.env.API_KEY,
|
||||
...url.parse(req.url, true).query,
|
||||
});
|
||||
const query = req.params.query;
|
||||
const results = await axios.get(
|
||||
`https://api.mapbox.com/geocoding/v5/mapbox.places/${query}.json?${params}`
|
||||
);
|
||||
|
||||
// format data to include city and state
|
||||
results.data.features.forEach((item) => {
|
||||
// init set to null
|
||||
item.city = null;
|
||||
item.state = null;
|
||||
|
||||
// cycle through content results
|
||||
item.context.forEach((type) => {
|
||||
if (type.id.includes("place")) {
|
||||
item.city = type.text;
|
||||
}
|
||||
if (type.id.includes("region")) {
|
||||
item.state = type.text;
|
||||
}
|
||||
});
|
||||
});
|
||||
const data = results.data;
|
||||
res.status(200).json(data);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in new issue
Block a user