快速开启node反向代理请求

  • Post author:
  • Post category:其他


新建目录 blocks

初始化 npm init -y

新建文件serve.js

相关依赖

express http-proxy-middleware

const express = require('express')
const app = express()
const { createProxyMiddleware } = require('http-proxy-middleware')
const host = 'localhost'
const port = '3000'
app.use('/', express.static('static')) //设置静态资源
app.use(
    '/api/*',
    createProxyMiddleware({
        target: 'https://www.jianshu.com/', //代理域名或ip
        changeOrigin: true,
        pathRewrite: {
            '^/api': '',  /api这一截将被替换掉
        },
    })
)
app.listen(port, function () {
    console.log('server is listening at http, host, port)
})

新建static文件夹 ,里面添加index.html

$.ajax({
    url: "/api/asimov/notes/e5031b0c5eb0/recommendations",
    success: () => {
    }
})



版权声明:本文为u013611033原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。