Most efficient monolith structure to have an Express.js REST API with Next.js SSR client?

https://stackoverflow.com/questions/78670127/most-efficient-monolith-starter-project-structure-to-have-an-express-js-rest-api Hi there! I'm starting a fulltack JS application, but would like to know your opinions on how an express.js server should be set for a Next.js frontend (Next.js API development is not an option for this purpose). I wonder if the following would be an appropiate approximation:
/project
|-- /node_modules
|-- /client
|-- /server
└- package.json
/project
|-- /node_modules
|-- /client
|-- /server
└- package.json
In tutorials I usually find the instruction to:
/project
|-- /client
| |-- /node_modules
| └- package.json
|-- /server
| |-- /node_modules
└ └- package.json
/project
|-- /client
| |-- /node_modules
| └- package.json
|-- /server
| |-- /node_modules
└ └- package.json
I'm not certain on how this would work in production either as I don't have experience in deployment or integration. Should the client and server process run as 2 different runtime threads or should a single index.js handle everything under a single mode_modules library for monolith. Something like... index.js
const app = express()

// Maybe run Next
app.get('/', (req, res) => {
// Run Next SSR like...
// res.send(next????)
})

app.get('/api', (req, res) => {
res.status(200)
.json({ resource: "value" })
})

app.listen(3000, () => "Express listening on port 3000")
const app = express()

// Maybe run Next
app.get('/', (req, res) => {
// Run Next SSR like...
// res.send(next????)
})

app.get('/api', (req, res) => {
res.status(200)
.json({ resource: "value" })
})

app.listen(3000, () => "Express listening on port 3000")
Appreciate your review and advice as I'm just an amateur programmer!
Stack Overflow
Most efficient monolith starter project structure to have an Expres...
I'm starting a fulltack JS application, but would like to know your opinions on how an express.js server should be set for a Next.js frontend (Next.js API development is not an option for this purp...
3 Replies
Ovidius
Ovidius2w ago
Hi, it's not clear to me what you're trying to do, but it seems you want to run a nextjs and express as different (or combined) instances with common modules and files. Not sure if it's even possible but I would not even try it. How I see it: Option 1 If you want a separate backend running with express, keep it as separate application in it's own repo. The frontend, nextjs, a different application in it's own repo. You run them both locally on different ports or containerized. Option 2 (probably easiest to manage) You do everything in the nextjs project since it has backend and frontend capabilities.
Jacob
Jacob2w ago
Going off waht @Ovidius said you could run in a monrepo, turbo is the best choice for nextjs https://turbo.build/repo then you have all the code in the same code base and simply import the package in your nextjs repo if you want to for example share types. example mono repos: - https://github.com/midday-ai/midday - https://github.com/projectx-codehagen/Badget - https://github.com/Codehagen/Dingify - https://github.com/vercel/turbo/tree/main/examples
Turbo
Turborepo
The build system that makes ship happen.
GitHub
GitHub - midday-ai/midday: Run your business smarter 🪄
Run your business smarter 🪄. Contribute to midday-ai/midday development by creating an account on GitHub.
GitHub
GitHub - projectx-codehagen/Badget: Badget aims to simplify financi...
Badget aims to simplify financial management with a user-friendly interface and robust backend - projectx-codehagen/Badget
GitHub
GitHub - Codehagen/Dingify: Dingify is helping you unlock the power...
Dingify is helping you unlock the power of seamless real-time monitoring - Codehagen/Dingify
GitHub
turbo/examples at main · vercel/turbo
Incremental bundler and build system optimized for JavaScript and TypeScript, written in Rust – including Turbopack and Turborepo. - vercel/turbo
A Reptilian
A Reptilian2w ago
Gotchou @Ovidius @Jacob , this brings a lot of clarification. Thank you so much!