Platform
Solutions
Resources
Pricing
Sign in
Sign up
Tom Larkworthy
Tech Lead at Taktile. Formerly Firebase, Google
Workspace
Fork
Published
By
Tom Larkworthy
Edited
Aug 12, 2022
1 fork
7 stars
1
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.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
suncalc
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
suntimesRoute
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
handleSuntimes
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
suncalcServer
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
testing
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
lastRequest
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
installHere
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML