I have a Next.js app with a custom server (Express) that does some stuff continuously and keeps some data in memory as long as the server is alive.
I want to access this data from the API calls (pages). What is the proper way to store and access it?
My server index file:
import { createServer } from 'http'
import express from 'express'
import { parse } from 'url'
import next from 'next'
import pinoHttp from 'pino-http'
const logger = pinoHttp()
const dev = process.env.NODE_ENV !== 'production'
const nextApp = next({ dev })
try {
const handle = nextApp.getRequestHandler()
const port = process.env.NODE_PORT || 3000
nextApp.prepare().then(() => {
const app = express()
app.locals.myArray = []
// Do stuff in the background that will update the array
createServer((request, response) => {
const parsedUrl = parse(request.url, true)
logger(request, response)
handle(request, response, parsedUrl)
}).listen(port, (error?: Error) => {
if (error) throw error
console.log(`Listening on: http://localhost:${port}`)
})
})
} catch (error) {
console.log(error)
process.exit(1)
}
My page:
import { Request, Response } from 'express'
export default async function (request: Request, response: Response): Promise<void> {
// Here, request.app and response.app are both undefined.
}