Published
Edited
Aug 12, 2022
1 fork
7 stars
Insert cell
Insert cell
Insert cell
suncalc = require("suncalc@1.9.0/suncalc.js")
Insert cell
suntimesRoute = new URLPattern({ pathname: "/suncalc" })
Insert cell
async function handleSuntimes(request) {
const { latitude, longitude, date } = await request.json();
const times = suncalc.getTimes(new Date(date), latitude, longitude);
return Promise.resolve(new Response(JSON.stringify(times), { status: 200 }));
}
Insert cell
// deploy server within Observable notebook
viewof suncalcServer = endpoint(
"default",
async (req, res) => {
debugger; // put a request debugger breakpoint in for demo purposes
viewof lastRequest.value = req.body; // Also write the incoming request into the lastRequest value
if (suntimesRoute.test(req.url)) return handleSuntimes(req);
else res.status(404).send("Not found");
},
{
// Switch on public livecoding so everybody
// can experience livecoding
livecode: "public"
}
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more