Skip to main content
Version: Next

Server Config

You can configure the behavior of the server via the server field of app spec:

main.wasp.ts
import { app } from "@wasp.sh/spec"
import { myMiddlewareConfigFn, mySetupFunction } from "./src/myServerSetupCode" with { type: "ref" }

export default app({
name: "MyApp",
server: {
setupFn: mySetupFunction,
middlewareConfigFn: myMiddlewareConfigFn,
},
// ...
})

Setup Functionโ€‹

Adding a Custom Routeโ€‹

As an example, adding a custom route would look something like:

src/myServerSetupCode.js
export const mySetupFunction = async ({ app }) => {
addCustomRoute(app)
}

function addCustomRoute(app) {
app.get("/customRoute", (_req, res) => {
res.send("I am a custom route")
})
}

Storing Some Values for Later Useโ€‹

In case you want to store some values for later use, or to be accessed by the Operations you do that in the setupFn function.

Dummy example of such function and its usage:

src/myServerSetupCode.js
let someResource = undefined

export const mySetupFunction = async () => {
// Let's pretend functions setUpSomeResource and startSomeCronJob
// are implemented below or imported from another file.
someResource = await setUpSomeResource()
startSomeCronJob()
}

export const getSomeResource = () => someResource
src/queries.js
import { getSomeResource } from "./myServerSetupCode.js"

...

export const someQuery = async (args, context) => {
const someResource = getSomeResource()
return queryDataFromSomeResource(args, someResource)
}
note

The recommended way is to put the variable in the same module where you defined the setup function and then expose additional functions for reading those values, which you can then import directly from Operations and use.

This effectively turns your module into a singleton whose construction is performed on server start.

For the full description of the setupFn field, check the Server API Reference.

Middleware Config Functionโ€‹

You can configure the global middleware via the middlewareConfigFn. This will modify the middleware stack for all operations and APIs.

Read more in the configuring middleware section.

API Referenceโ€‹

API reference

Server ยป

All the options for the server field of the app spec.