美文网首页
node解决跨域

node解决跨域

作者: coderfl | 来源:发表于2022-07-18 15:16 被阅读0次
    • 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解决跨域

          本文链接:https://www.haomeiwen.com/subject/vztsirtx.html