Skip to content

Instantly share code, notes, and snippets.

@tlockney
Last active July 3, 2024 05:26
Show Gist options
  • Save tlockney/b3197ab22771ddaeb8bdc384c7873132 to your computer and use it in GitHub Desktop.
Save tlockney/b3197ab22771ddaeb8bdc384c7873132 to your computer and use it in GitHub Desktop.
A quick example of Deno.serve
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Deno.serve Example\n",
"\n",
"Testing out whether I can run `Deno.serve` in a Jupyter notebook."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{ deno: \u001b[32m\"1.44.4+3324d72\"\u001b[39m, v8: \u001b[32m\"12.7.224.9\"\u001b[39m, typescript: \u001b[32m\"5.4.5\"\u001b[39m }"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Deno.version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Listening on http://localhost:9999/\n"
]
}
],
"source": [
"let server = Deno.serve({ port: 9999, hostname: \"0.0.0.0\" }, (req: Request) => {\n",
" return new Response(\"Hello world\");\n",
"});"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This seems to work fine:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response {\n",
" body: ReadableStream { locked: false },\n",
" bodyUsed: false,\n",
" headers: Headers {\n",
" \"content-length\": \"11\",\n",
" \"content-type\": \"text/plain;charset=UTF-8\",\n",
" date: \"Wed, 03 Jul 2024 05:19:16 GMT\",\n",
" vary: \"Accept-Encoding\"\n",
" },\n",
" ok: true,\n",
" redirected: false,\n",
" status: 200,\n",
" statusText: \"OK\",\n",
" url: \"http://localhost:9999/\"\n",
"}\n"
]
}
],
"source": [
"let resp = await fetch('http://localhost:9999');\n",
"console.log(resp);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This seems to work, too, oddly:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
]
}
],
"source": [
"const command = new Deno.Command(\"curl\", {\n",
" args: [\"-v\", \"localhost:9999\"]\n",
"});\n",
"\n",
"const { stdout } = await command.output();\n",
"console.log(new TextDecoder().decode(stdout));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But if I run the following in a shell\n",
"```sh\n",
"curl -v localhost:9999\n",
"```\n",
"It just hangs\n",
"```sh\n",
"* Trying 127.0.0.1:9999...\n",
"* Connected to localhost (127.0.0.1) port 9999 (#0)\n",
"> GET / HTTP/1.1\n",
"> Host: localhost:9999\n",
"> User-Agent: curl/7.81.0\n",
"> Accept: */*\n",
"> \n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno (Canary)",
"language": "typescript",
"name": "deno-canary"
},
"language_info": {
"codemirror_mode": "typescript",
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"name": "typescript",
"nbconvert_exporter": "script",
"pygments_lexer": "typescript",
"version": "5.4.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment