node解决跨域

  • server.js
npm需要安装以下3个依赖(express,connect-history-api-fallback,http-proxy-middleware)
启动代理服务:node ./server.js
//pm2启动项目配置
const express = require('express')
const app = express();
//解决history路由404问题
const history = require('connect-history-api-fallback')
app.use(history())
//页面刷新问题处理
const path = require('path');
app.use(express.static(path.join(__dirname, '/')))
//跨域代理配置
const {createProxyMiddleware} = require("http-proxy-middleware")
app.use(createProxyMiddleware('/api', {
    changeOrigin: true,
    target: 'http://10.10.10.10:8088',
    pathRewrite: {'^/api': ''}
}))
//监听端口
app.listen(3333, error => error ? console.log(error) : console.log('start success!'))

你可能感兴趣的:(node解决跨域)