1

I am trying to serve different applications (two made with React and one API made with Express.JS)

In fact, the API works fine and it works in "/api" route. What I need to do is the following:

  • The Frontend app must be in "/" path
  • The admin panel must be in "/admin" (or whatever)

If I set any React app to serve in "/" it works fine, but if I set the other app in "/subfolder" it doesn't works

This is my server.js

const express = require('express')
const cors = require('cors')
const path = require('path')

const app = express()
const port = process.env.PORT || 3001

app.use(express.urlencoded({ extended:true }));
app.use(express.json());

//use cors

// Import DB Connection
require("/db/pathConfig");

// Import routes
const apiRoutes = require('routePath');
const usersRoutes = require('routePath');

app.use('/', express.static(__dirname + '/FE', {redirect: true}));
app.use('/admin', express.static(__dirname + '/adPanel'));
app.use('/api', apiRoutes);
app.use('/users', usersRoutes);
app.use('/uploads', express.static('uploads', {redirect: false}))

app.get('/*', (req, res) => {
  return res.sendFile(path.resolve('FE/index.html'))
})

/* app.get('/landing/*', (req, res) => {
  return res.sendFile(path.resolve('landing/index.html'))
}) */

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

0 Answers0