Server Config
You can configure the behavior of the server via the server field of app declaration:
- JavaScript
- TypeScript
appMyApp{
title: "My app",
// ...
server: {
setupFn: import{ mySetupFunction }from"@src/myServerSetupCode",
middlewareConfigFn: import{ myMiddlewareConfigFn }from"@src/myServerSetupCode"
}
}
appMyApp{
title: "My app",
// ...
server: {
setupFn: import{ mySetupFunction }from"@src/myServerSetupCode",
middlewareConfigFn: import{ myMiddlewareConfigFn }from"@src/myServerSetupCode"
}
}
Setup Functionβ
Adding a Custom Routeβ
As an example, adding a custom route would look something like:
- JavaScript
- TypeScript
exportconstmySetupFunction=async({ app })=>{
addCustomRoute(app)
}
functionaddCustomRoute(app){
app.get('/customRoute',(_req, res)=>{
res.send('I am a custom route')
})
}
import{ ServerSetupFn }from'wasp/server'
import{ Application }from'express'
exportconst mySetupFunction:ServerSetupFn=async({ app })=>{
addCustomRoute(app)
}
functionaddCustomRoute(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:
- JavaScript
- TypeScript
let someResource =undefined
exportconstmySetupFunction=async()=>{
// Let's pretend functions setUpSomeResource and startSomeCronJob
// are implemented below or imported from another file.
someResource =awaitsetUpSomeResource()
startSomeCronJob()
}
exportconstgetSomeResource=()=> someResource
import{ getSomeResource }from'./myServerSetupCode.js'
...
exportconstsomeQuery=async(args, context)=>{
const someResource =getSomeResource()
returnqueryDataFromSomeResource(args, someResource)
}
import{typeServerSetupFn}from'wasp/server'
let someResource =undefined
exportconst mySetupFunction:ServerSetupFn=async()=>{
// Let's pretend functions setUpSomeResource and startSomeCronJob
// are implemented below or imported from another file.
someResource =awaitsetUpSomeResource()
startSomeCronJob()
}
exportconstgetSomeResource=()=> someResource
import{typeSomeQuery}from'wasp/server/operations'
import{ getSomeResource }from'./myServerSetupCode.js'
...
exportconst someQuery: SomeQuery<...>=async(args, context)=>{
const someResource =getSomeResource()
returnqueryDataFromSomeResource(args, someResource)
}
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β
- JavaScript
- TypeScript
appMyApp{
title: "My app",
// ...
server: {
setupFn: import{ mySetupFunction }from"@src/myServerSetupCode",
middlewareConfigFn: import{ myMiddlewareConfigFn }from"@src/myServerSetupCode"
}
}
appMyApp{
title: "My app",
// ...
server: {
setupFn: import{ mySetupFunction }from"@src/myServerSetupCode",
middlewareConfigFn: import{ myMiddlewareConfigFn }from"@src/myServerSetupCode"
}
}
app.server is a dictionary with the following fields:
-
setupFn: ExtImportβsetupFndeclares a 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
setupFnfunction receives theexpress.Applicationand thehttp.Serverinstances as part of its context. They can be useful for setting up any custom server logic.- JavaScript
- TypeScript
src/myServerSetupCode.jsexportconstmySetupFunction=async()=>{
awaitsetUpSomeResource()
}Types for the setup function and its context are as follows:
wasp/serverexporttypeServerSetupFn=(context: ServerSetupFnContext)=>Promise<void>
exporttypeServerSetupFnContext={
app: Application // === express.Application
server: Server // === http.Server
}src/myServerSetupCode.tsimport{typeServerSetupFn}from'wasp/server'
exportconst mySetupFunction:ServerSetupFn=async()=>{
awaitsetUpSomeResource()
} -
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.