node+express部署多套vue3项目,总404页面由node控制,子404页面由子vue控制,node路由重定向

const express = require('express')
const history = require('connect-history-api-fallback')
const { createProxyMiddleware } = require('http-proxy-middleware')
const cors = require('cors')


let app = express()


app.use(cors())
app.use(history())

// //匹配api开头的请求,实际转发的请求保api这三个字母
// app.use(
//   '/api',
//   createProxyMiddleware({
//     target: 'http://localhost:85',
//     changeOrigin: true,
//   })
// )

// app.use(
//   '/master',
//   createProxyMiddleware({
//     target: 'http://10.30.251.100:81',
//     secure: false,
//     changeOrigin: true,
//     pathRewrite: function (path, req) {
//       console.log(path)
//       // 可以在这里重写路径
//       return `/master${path}`
//     },
//     on: {
//       proxyReq: (proxyReq, req, res) => {
//         //console.log(proxyReq)
//         /* handle proxyReq */
//       },
//       proxyRes: (proxyRes, req, res) => {
//         //console.log(proxyRes)
//         /* handle proxyRes */
//       },
//       error: (err, req, res) => {
//         console.log(err)
//         /* handle error */
//       },
//     },
//   })
// )

// app.use(
//   '/product',
//   createProxyMiddleware({
//     target: 'http://10.30.251.100:82',
//     secure: false,
//     changeOrigin: true,
//     pathRewrite: function (path, req) {
//       console.log(path)
//       // 可以在这里重写路径
//       return `/product${path}`
//     },
//     on: {
//       proxyReq: (proxyReq, req, res) => {
//         //console.log(proxyReq)
//         /* handle proxyReq */
//       },
//       proxyRes: (proxyRes, req, res) => {
//         //console.log(proxyRes)
//         /* handle proxyRes */
//       },
//       error: (err, req, res) => {
//         console.log(err)
//         /* handle error */
//       },
//     },
//   })
// )

app.use(
  '/api',
  createProxyMiddleware({
    target: 'http://127.0.0.1:8001',
    secure: false,
    changeOrigin: true,
    pathRewrite: function (path, req) {
      console.log('path', path)
      // 可以在这里重写路径
      return `${path}`
    },
    on: {
      proxyReq: (proxyReq, req, res) => {
        //console.log(proxyReq)
        /* handle proxyReq */
      },
      proxyRes: (proxyRes, req, res) => {
        //console.log(proxyRes)
        /* handle proxyRes */
      },
      error: (err, req, res) => {
        console.log(err)
        /* handle error */
      },
    },
  })
)

// app.use(
//   '/main',
//   createProxyMiddleware({
//     target: 'http://127.0.0.1:9000',
//     secure: false,
//     changeOrigin: true,
//     pathRewrite: function (path, req) {
//       console.log('path', path)
//       // 可以在这里重写路径
//       return `${path}`
//     },
//     on: {
//       proxyReq: (proxyReq, req, res) => {
//         //console.log(proxyReq)
//         /* handle proxyReq */
//       },
//       proxyRes: (proxyRes, req, res) => {
//         //console.log(proxyRes)
//         /* handle proxyRes */
//       },
//       error: (err, req, res) => {
//         console.log(err)
//         /* handle error */
//       },
//     },
//   })
// )

// app.use('/main', express.static('./dist'))



app.use('/main', express.static('./main'))
app.use('/sub/', express.static('./sub'))
app.use('/', (req, res) => {
  res.redirect('/main/index.html');
});

app.get('*', (req, res) => {
  res.redirect('/main/index.html#/404');
});






app.listen(99, () => {
  console.log('http://localhost:99')
  console.log('success')
})

人工智能学习网站

https://chat.xutongbao.top

猜你喜欢

转载自blog.csdn.net/xutongbao/article/details/142355482