账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于在用Vue-cli搭建的知乎日报项目,在生产环境下启用代理的问题?
    47
    0

    我通过vue-cli搭建了一个知乎日报项目,在打开页面前,需要先通过node proxy.js打开代理,
    在开发环境下我是直接在package.json中通过"dev": "node proxy.js & webpack-dev-server --inline --progress --config build/webpack.dev.conf.js"打开的。
    在生产环境中我尝试在App.vue中通过created () {node proxy.js}报错了,我不知道能不能在js中直接调用node命令,如果能的话,我的错误在哪?不能的话,我该着么做?求告知,谢!

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 在使用Vue CLI搭建的知乎日报项目中,为了启用代理,你可以使用npm的proxy模块来配置代理。以下是一个简单的例子:

      首先,你需要安装proxy模块:

      bash npm install proxy

      然后,你可以创建一个proxy.json文件,配置代理:

      json { "ProxyTable": { "http": { "address": "http://127.0.0.1:8080", "changeOrigin": true, "passThrough": true }, "https": { "address": "https://127.0.0.1:8080", "changeOrigin": true, "passThrough": true } } }

      在这个例子中,httphttps代理设置为在本地服务器8080端口上监听请求,然后通过代理服务器8081端口进行转发。

      最后,你可以使用以下代码启动代理:

      ```javascript import http from 'http' import https from 'https'

      const proxy = { target: 'http://127.0.0.1:8080', changeOrigin: true, passThrough: true }

      const server = http.createServer((req, res) => { proxy.target === 'http' ? res.writeHead(200, { 'Content-Type': 'text/html' }) : res.writeHead(200, { 'Content-Type': 'text/plain' }) proxy.target === 'https' ? res.writeHead(200, { 'Content-Type': 'text/html' }) : res.writeHead(200, { 'Content-Type': 'text/plain' }) proxy.target === 'http' ? res.write(Hello, world from http://127.0.0.1:8080) : res.write(Hello, world from https://127.0.0.1:8080) res.end() })

      server.listen(3000, () => { console.log('Server running on port 3000') }) ```

      在这个例子中,当请求从http://localhost:3000发送到代理服务器时,请求会被转发到http://127.0.0.1:8080,并返回Hello, world from http://127.0.0.1:8080

      当请求从https://localhost:3000发送到代理服务器时,请求会被转发到https://127.0.0.1:8080,并返回Hello, world from https://127.0.0.1:8080

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部