====================== Updated docs/advanced/apis to 0.12. (#1729) Updated docs/advanced/middleware-config to 0.12. (#1730) Updated docs/advanced/links to 0.12. (#1732) Updated docs/advanced/web-sockets to 0.12. (#1733) Updated docs/advanced/email to 0.12. (#1734) Auth docs rework (#1723) Updated docs/advanced/jobs to 0.12. (#1740) Updates starter templates (#1744) Updates general docs (#1745) Update customising app docs (#1748) Updates client setup (#1749) Change server config docs (#1750) Updates CSS frameworks docs (#1753) Updates static assets docs (#1751) Updates custom vite config docs (#1754) Update testing docs (#1752) Updated docs/introduction/* to 0.12. (#1756) Restructuring data model docs (#1739) Updates depedencies docs (#1768) Co-authored-by: Mihovil Ilakovac <mihovil@ilakovac.com>
6.5 KiB
title |
---|
Server Config |
import { ShowForTs, ShowForJs } from "@site/src/components/TsJsHelpers";
You can configure the behavior of the server via the server
field of app
declaration:
app MyApp {
title: "My app",
// ...
server: {
setupFn: import { mySetupFunction } from "@src/myServerSetupCode.js",
middlewareConfigFn: import { myMiddlewareConfigFn } from "@src/myServerSetupCode.js"
}
}
app MyApp {
title: "My app",
// ...
server: {
setupFn: import { mySetupFunction } from "@src/myServerSetupCode.js",
middlewareConfigFn: import { myMiddlewareConfigFn } from "@src/myServerSetupCode.js"
}
}
Setup Function
setupFn
declares a Typescript function that will be executed on server start.
setupFn
declares a Javascript function that will be executed on server start.
Adding a Custom Route
As an example, adding a custom route would look something like:
export const mySetupFunction = async ({ app }) => {
addCustomRoute(app)
}
function addCustomRoute(app) {
app.get('/customRoute', (_req, res) => {
res.send('I am a custom route')
})
}
import { ServerSetupFn } from 'wasp/server'
import { Application } from 'express'
export const mySetupFunction: ServerSetupFn = async ({ app }) => {
addCustomRoute(app)
}
function addCustomRoute(app: Application) {
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:
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
import { getSomeResource } from './myServerSetupCode.js'
...
export const someQuery = async (args, context) => {
const someResource = getSomeResource()
return queryDataFromSomeResource(args, someResource)
}
import { type ServerSetupFn } from 'wasp/server'
let someResource = undefined
export const mySetupFunction: ServerSetupFn = async () => {
// Let's pretend functions setUpSomeResource and startSomeCronJob
// are implemented below or imported from another file.
someResource = await setUpSomeResource()
startSomeCronJob()
}
export const getSomeResource = () => someResource
import { type SomeQuery } from 'wasp/server/operations'
import { getSomeResource } from './myServerSetupCode.js'
...
export const someQuery: 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. :::
Read more about server setup function below.
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 about middleware config function below.
API Reference
app MyApp {
title: "My app",
// ...
server: {
setupFn: import { mySetupFunction } from "@src/myServerSetupCode.js",
middlewareConfigFn: import { myMiddlewareConfigFn } from "@src/myServerSetupCode.js"
}
}
app MyApp {
title: "My app",
// ...
server: {
setupFn: import { mySetupFunction } from "@src/myServerSetupCode.js",
middlewareConfigFn: import { myMiddlewareConfigFn } from "@src/myServerSetupCode.js"
}
}
app.server
is a dictionary with the following fields:
-
setupFn: ExtImport
setupFn
declares a TypescriptJavascript function that will be executed on server start. This function is expected to be async and will be awaited before the server starts accepting any requests.It allows you to do any custom setup, e.g. setting up additional database/websockets or starting cron/scheduled jobs.
The
setupFn
function receives theexpress.Application
and thehttp.Server
instances as part of its context. They can be useful for setting up any custom server logic.export const mySetupFunction = async () => { await setUpSomeResource() }
Types for the setup function and its context are as follows:
export type ServerSetupFn = (context: ServerSetupFnContext) => Promise<void> export type ServerSetupFnContext = { app: Application // === express.Application server: Server // === http.Server }
import { type ServerSetupFn } from 'wasp/server' export const mySetupFunction: ServerSetupFn = async () => { await setUpSomeResource() }
-
middlewareConfigFn: ExtImport
The import statement to an Express middleware config function. This is a global modification affecting all operations and APIs. See more in the configuring middleware section.