27 lines
686 B
JavaScript
27 lines
686 B
JavaScript
import express from "express";
|
|
import fetch from "node-fetch";
|
|
|
|
const router = express.Router();
|
|
|
|
// GET /api/autocomplete?input=...
|
|
router.get("/", async (req, res) => {
|
|
const { input } = req.query;
|
|
if (!input) return res.status(400).json({ predictions: [] });
|
|
|
|
const url =
|
|
`https://maps.googleapis.com/maps/api/place/autocomplete/json` +
|
|
`?input=${encodeURIComponent(input)}` +
|
|
`&key=${process.env.GOOGLE_PLACES_API_KEY}` +
|
|
`&types=address`;
|
|
|
|
try {
|
|
const r = await fetch(url);
|
|
const data = await r.json();
|
|
res.json(data);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
export default router;
|