国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > JS > 正文

使用express來(lái)代理服務(wù)的方法

2024-05-06 16:52:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

nodejs和nginx都可以反向代理,解決跨域問(wèn)題。

本地服務(wù)

const express = require('express')const app = express()//如果它在最前面,后面的/開頭的都會(huì)被攔截app.get('/', (req, res) => res.send('Hello World!'))app.use(express.static('public'));//靜態(tài)資源app.use('/dist', express.static(path.join(__dirname, 'public')));//靜態(tài)資源//404app.use('/test', function (req, res, next) {  res.status(404).send("Sorry can't find that!");});app.use(function (req, res, next) {  //TODO 中間件,每個(gè)請(qǐng)求都會(huì)經(jīng)過(guò)  next();});app.use(function (err, req, res, next) {  //TODO 失敗中間件,請(qǐng)求錯(cuò)誤后都會(huì)經(jīng)過(guò)  console.error(err.stack);  res.status(500).send('Something broke!');  next();});app.listen(4000, () => console.log('Example app listening on port 4000!'))

與request配合使用

這樣就將其它服務(wù)器的請(qǐng)求代理過(guò)來(lái)了

const request = require('request');app.use('/base/', function (req, res) {  let url = 'http://localhost:3000/base' + req.url;  req.pipe(request(url)).pipe(res);});

使用http-proxy-middleware

const http_proxy = require('http-proxy-middleware');const proxy = { '/tarsier-dcv/': {  target: 'http://192.168.1.190:1661' }, '/base/': {  target: 'http://localhost:8088',  pathRewrite: {'^/base': '/debug/base'} }};for (let key in proxy) { app.use(key, http_proxy(proxy[key]));}

監(jiān)聽本地文件變化

使用nodemon插件。

--watch test指監(jiān)聽根目錄下test文件夾的所有文件,有變化就會(huì)重啟服務(wù)。

"scripts": { "server": "nodemon --watch build --watch test src/server.js"}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永州市| 自治县| 崇仁县| 徐汇区| 保靖县| 顺昌县| 蛟河市| 浦东新区| 宁蒗| 高平市| 恩施市| 宜城市| 张家港市| 鄂尔多斯市| 连平县| 乐至县| 广昌县| 油尖旺区| 兴隆县| 盐池县| 嘉黎县| 祁连县| 固始县| 汽车| 滨海县| 罗平县| 开原市| 汶上县| 开平市| 康定县| 鹤峰县| 鞍山市| 萨迦县| 沂南县| 加查县| 师宗县| 洛隆县| 托克托县| 松滋市| 石城县| 垫江县|