Skip to content

Instantly share code, notes, and snippets.

@martinvol
Created October 8, 2024 13:28
Show Gist options
  • Save martinvol/cfc8579b95f41aa108fb2ac1143cb9a8 to your computer and use it in GitHub Desktop.
Save martinvol/cfc8579b95f41aa108fb2ac1143cb9a8 to your computer and use it in GitHub Desktop.
Integrating Next.js + Express (with Admin.Js as example)
import next from "next"
import express from 'express'
import AdminJS from 'adminjs'
import AdminJSExpress from '@adminjs/express'
import { Database, Resource, getModelByName } from '@adminjs/prisma'
import { PrismaClient } from '@prisma/client'
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const PORT = 3000
const nextApp = next({ dev, hostname, port: PORT })
const nextHandler = nextApp.getRequestHandler()
const prisma = new PrismaClient()
AdminJS.registerAdapter({ Database, Resource })
const start = async () => {
await nextApp.prepare()
const app = express()
const adminOptions = {
resources: [{
resource: { model: getModelByName('Model1'), client: prisma },
options: {},
},{
resource: { model: getModelByName('Model2'), client: prisma },
options: {},
}
],
}
const admin = new AdminJS(adminOptions)
const adminRouter = AdminJSExpress.buildRouter(admin)
// Use AdminJS routes
app.use(admin.options.rootPath, adminRouter)
// Handle all other routes with Next.js
app.all('*', (req, res) => {
return nextHandler(req, res)
})
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`)
console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment