fastify-dx icon indicating copy to clipboard operation
fastify-dx copied to clipboard

How to run code on the server

Open simoneb opened this issue 2 years ago • 0 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the issue has not already been raised

Issue

I'm playing around with this library to figure out how I would create something that I would normally use a SPA for.

Example

  • a Web UI with an HTTP API which the client invokes, runs something on the server and returns some results
  • let's also assume that what runs on the server cannot run on the client (e.g. a Node-only library which doesn't make sense running on the client)

What I did

  1. created a "API" module containing the code I would normally have in my backend API (let's say it's some Node.js code, e.g. DB access)
  2. created a new page which imports the API module
  3. set the page as server-only so that my API code doesn't run on the client
  4. use the API module in the getData export of the page module

Issues I encountered

  1. Vite cannot build the app because the API module cannot be built on the client (although the page where I use such code is set a serverOnly)
  2. Even assuming the API module code successfully builds on the client, how do I make it run on the server only, e.g. if it needs client inputs to run?

simoneb avatar Jul 30 '22 13:07 simoneb