D
Deno

help

Deno Deploy - is it possible to have 2 servers running?

Wwingerlang10/5/2023
Maybe Im using it completely wrong, but locally when i develop I have two Deno servers running: Backend - deno server at port 8080 Frontend - Deno / Fresh server at port 8000 My backend is mainly used as a standalone REST-API. My fronend then sends fetch-requests to the API. The problem is this. At deno deploy, I can only point to one server. How do I come around this? Thanks in advance!
Mmarvinh.10/5/2023
Deno deploy is a serverless function platform. So there isn't a server running in the classic sense. You can certainly create multiple projects (each project = 1 function) and have them communicate with each other. Given that the backend is likely closely tied to the frontend it may make more sense to run them both in the same function though
Wwingerlang10/5/2023
Thanks for the reply! Isn't it more pure to seperate them? For instance, I could create an mobile app or text interface that only need the data, seems bad practice's to merge them. But in my case, since they are both written in TS i obviously share some code and they currently share repo. Seems like this use case is not supported? Or should I have two deno projects that point to the same repo but with different entry points?
Mmarvinh.10/5/2023
The use case is supported. Instead of creating two servers, create two functions I don't think it's bad practice. For projects that only have one UI it's totally fine. If you have multiple UIs an API abstraction layer may make sense
Wwingerlang10/5/2023
Yeah, and I might have two UI soon. But Ive read some about Deno but obviously im still very noobish - what is even "two functions" refering to here? I mean the backend is deno http with Oak, starting like this: await app.listen({ port: 8080 }); The frontend is in another folder all together and starts like this: await start(manifest, config);
Mmarvinh.10/5/2023
you can think of a serverless function as being a server that spins up when needed and shuts down when it has done it's thing and no new request comes in. In very laymans terms it's basically a short lived server In your case you'd create one deploy project with the oak server and one for fresh and have them talk to each other Note that having them both in the same function would likely be better for performance. There can be multiple functions of the same project running at the same time. So there is no need for manual server scaling like you'd do in a classic setup
Wwingerlang10/5/2023
Thanks for the very detailed explanations! But lets say Ill go for that strat in the beginning, how do I wire up the code? Right now, I start the REST-API as a ordinary deno server, and then I have the isolated Fresh app in /frontend/(main.ts)?
Mmarvinh.10/5/2023
Both Fresh and oak expose a .handler() method. So you can either create a socket via Deno.serve() and branch off to oak or Fresh manually, or spin up oak inside Fresh as a /api middleware
Wwingerlang10/5/2023
Hmm alright, sounds very promising. Ill try to find some online resource to read more about that particular problem. My goto strategy is traversing lots of youtube-guides, but very lacking thus far! Sorry for not understanding this concept that well. Just nu sure how to "spin up" a fresh-instance or oak. I'm reading as much as I can to try figure it out, but so far no success
Ffro.profesional10/5/2023
For oak you should use the handler method https://github.com/oakserver/oak#handle-method You can chose to use it inside your fresh app Or you can use the Deno.serve to pass the request https://deno.land/api?s=Deno.serve
Deno
Deno.serve | Runtime APIs | Deno
Serves HTTP requests with the given handler.
GitHub
GitHub - oakserver/oak: A middleware framework for handling HTTP wi...
A middleware framework for handling HTTP with Deno 🐿️ 🦕 - GitHub - oakserver/oak: A middleware framework for handling HTTP with Deno 🐿️ 🦕
Wwingerlang10/5/2023
Thanks for the reply. I think the documentation is written in a way that it presumes some knowledge I do not have. I cannot really connect the dots how I glue my frontend with the backend using oak/deno. Should I somehow move the "await start(manifest, config);" Im using to start the Fresh app, in some handler-method along side the await app.listen({ port: 8080 }); Im already using for the backend?
Ffro.profesional10/5/2023
The easy way, would be create a route “api” in your fresh app, since the route gets a Request, you can directly pass the req obj to your Oak handler Have you tried that?
Wwingerlang10/6/2023
Im not sure how to "pass the req object" to the Oak handler. Every time I look at code it seems a bit different. I actually still dont get if I can/should have two servers to separate frontend/backend or if you mean that I should just move all backend-code into the frontend somehow. Im trying hard to understand but just dont get it yet, sorry for that.
Ffro.profesional10/6/2023
import { Application } from "https://deno.land/x/oak@v12.6.1/mod.ts";

const app = new Application();

app.use((ctx) => ctx.response.body = "Hello world");

Deno.serve(async (req) => {
return (await app.handle(req))!
})
import { Application } from "https://deno.land/x/oak@v12.6.1/mod.ts";

const app = new Application();

app.use((ctx) => ctx.response.body = "Hello world");

Deno.serve(async (req) => {
return (await app.handle(req))!
})
@wingerlang do you understand what is happening here? Do you see Deno.serve starts the server and has an obj that is the request?
Deno.serve(async (req) => {
Deno.serve(async (req) => {
You can pass that request to your Oak app:
await app.handle(req)
await app.handle(req)
And it will run whatever you have in your app, based on the request, and will return a response The important part is that you can use your Oak app as long as you pass the Request, and it will give you a Response Now, on your fresh app, you can create a route that has a handler, docs: https://fresh.deno.dev/docs/getting-started/custom-handlers. From the docs:
The handler has access to the Request object that backs the request to the route and must return a Response object.
That is essentially what this does:
await app.handle(req)
await app.handle(req)
So with the above example, you can create a Fresh route similar to this:
import { Application } from "https://deno.land/x/oak@v12.6.1/mod.ts";
+import { Handler } from "$fresh/server.ts";

const app = new Application();
app.use((ctx) => ctx.response.body = "Hello world");

-Deno.serve(async (req) => {
- return (await app.handle(req))!
-})

+export const handler: Handler = async (req) => {
+ return (await app.handle(req))!
+}
import { Application } from "https://deno.land/x/oak@v12.6.1/mod.ts";
+import { Handler } from "$fresh/server.ts";

const app = new Application();
app.use((ctx) => ctx.response.body = "Hello world");

-Deno.serve(async (req) => {
- return (await app.handle(req))!
-})

+export const handler: Handler = async (req) => {
+ return (await app.handle(req))!
+}
Wwingerlang10/9/2023
Thanks for the reply! I will for sure deep dive on the code snippets tomorrow 🙂

Looking for more? Join the community!

Recommended Posts
NPM package randomly brokeI've been using the imagekit package for my fresh project, and it's been working fine since I instalsubpackage imports with npm specifierse.g. `import colors from "npm:tailwindcss/colors"` currently fails, is this not supposed to work? SIs it possible to use Cache-Control headers to control Deno's cacheI've seen via an issue ( https://github.com/denoland/deno/issues/16296 ) that Deno stores its metadaDeno Deploy Queue trouble in productionI'm encountering an issue with Deno Queue in my Deno Deploy project, and I'm hoping to get some guidDeno compile target errorHello, I am trying to compile a Deno api using `deno --unstable ./path/to/api/index.ts --output ./paHow to avoid the method POST to executed in the URL I am redirected to?How to avoid the method POST to executed in the URL I am redirected to? routes/admin/login.tsx ```thow to use Untar without deprecated functions?`Untar` from std/archives requires an instance of `Reader` - apparently most functions return some RHelp, does the middleware example in the Deno Fresh documentation not work for anyone else?I tried to follow the example in Deno Fresh about middlewares, but it doesn't work, the content `ctxDeno Cache locationHello, I am trying to cache my dependencies during the compile action using Github Actions, where doHow to build rusty_v8 with old v8 version?Hello all ! I am trying to build rusty_v8 (https://github.com/denoland/rusty_v8) with an old versionCan someone explain WebSocketStream to me?Whenever I try to connect using WebSocketStream and the host doesn't respond, my program inevitably deno doc: error: fp-tsthat's pretty much all I have :/ Running `deno doc Server.ts` only yields `error: fp-ts`. Can I enabI want to add my package xmlbuilder2 to deno.landBut somebody already added it, albeit with his own forked repository (now non-existent, thus unusablCache imports in deno.jsonIs there a way to cache imports defined in deno.json? I am hoping to avoid additional deps.ts file.typescript type definition for deno.jsonDoes it exist, and where can I get it?Publishing (and configuring) the webhook for deno.land from monorepoThanks in advance for your help! I went through the setup on https://deno.com/add_module for my jupLooking for a Dev who is well-versed in Fast-Delivery of building front-end pages using React.js forLooking for a Dev who is well-versed in Fast-Delivery of building front-end pages using React.js forHow to assert object with a field of a given type?Hi, I'm writing an integration test that needs to assert an object response, where the ID field can Deno + LitHey how can use the [Lit](https://lit.dev/) framework with Deno? Is there a guide or config on how tIssues with Crypto Subtle Digest ext:deno_crypto/00_crypto.jsCurrently we are using `deno_core v0.200.0` `deno_runtime v0.123.0` when attempting to invoke `crypt