I’m working on a project hosted on an AWS Windows EC2 instance. Right now my frontend is accessible on port 80 and the backend runs on port 5000. I’m looking for a way to serve both from the same port without needing to specify a separate port for the API calls.
Currently, it looks like this:
- Frontend: mysite.com
- Backend: mysite.com:5000
What I want is something like:
- Backend: mysite.com/api/
Is there a way to set this up using a single server file (like app.js or main.js) that handles both frontend and backend routing? I’m not sure how to configure this properly. Any tips or examples would be really helpful!
Here’s a basic structure I’m thinking of:
const express = require('express')
const app = express()
// Frontend routes
app.get('/', (req, res) => {
res.send('Welcome to the homepage')
})
// Backend routes
app.get('/api/users', (req, res) => {
res.json({ users: ['Alice', 'Bob', 'Charlie'] })
})
app.listen(80, () => console.log('Server running on port 80'))
Is this on the right track? How can I improve it to achieve what I’m after?