使用gulp相关插件进行api的跨域访问并监控文件的变化的简单处理

使用Node.js和Gulp CLI构建前端开发工程 Node.js是一个基于Chrome V8引擎的JavaScript运行环境,它允许我们在服务器端执行JavaScript代码,与传统基于Java的服务器技术形成鲜明对比。Node.js被设计为高度可扩展和非阻塞I/O的,使其非常适合处理大量的并发连接,例如实时应用或网络应用。Node.js的非阻塞I/O模型和事件驱动架构使得它在处理高并发数据流时表现突出,尤其适合I/O密集型的应用场景。这包括社交媒体应用、实时通信平台以及API服务等。 阅读详情

跨域处理很麻烦,特别是针对低版本的IE,并且即使做了相关处理,还可能存在安全问题。在开发阶段,我们在不使用工具的情况下以及现代浏览器的环境下,jsonp是一个很好的方式,但是jsonp只能针对get方式的请求,并不完美,这里我们完全可以利用gulp组件进行api的转发处理,以期在开发阶段简单搞定问题。

重要的两款插件介绍

需要的相关依赖

  • $ npm i -g gulp
  • $ npm i --save-dev gulp gulp-connect http-proxy-middleware gulp-plumber shelljs run-sequence

关键代码

//引入插件
var gulp = require('gulp');
var connect = require('gulp-connect');
var proxy = require('http-proxy-middleware');
var plumber = require('gulp-plumber');
var sh = require('shelljs');
var runSequence = require('run-sequence');

// 需要监控的路径
var watchPath = ['src'];

// 使用connect启动一个Web服务器
gulp.task('connect', function () {
    connect.server({
        root: '',
        livereload: true,
        port: 9000,
        middleware: function (connect, opt) {
            return [
                proxy(["/api"], {
                    target: '这里写你的服务器api的地址如:192.168.1.100',
                    changeOrigin: true,
                    pathRewrite: {
                        '^/api': '/'
                    }
                })
            ]
        }
    });
});

// 监控文件的变动
gulp.task('watch', function () {
    gulp.watch(watchPath, ['watchCallback']);
});

// 监控的回调reload
gulp.task('watchCallback', function () {
    gulp.src(watchPath)
        .pipe(plumber())
        .pipe(connect.reload());
});

//运行Gulp时,默认的Task
gulp.task('server', function () {
    runSequence(['connect', 'watch'], function () {
       sh.echo("跨域服务器开启!");
    });
});

开启跨域服务器

  • $ gulp server

测试访问api

gulp解决跨域的配置文件问题 下面小编就为大家带来一篇gulp解决跨域的配置文件问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 立即下载

相关推荐

【机器人学习】abb工业机器人运动学分析与圆弧轨迹规划(三维模型+matlab代码)

下载链接 solidworks模型 D-H建模 圆仿真过程 视频过程:

yjw0911的博客 2533

gulp 跨域

//引入插件 var gulp = require('gulp'); // var Proxy = require('gulp-connect-proxy'); var connect = require('gulp-connect'); var proxy = require('http-proxy-middleware'); //使用connect启动一个W...

di84186的博客 383

导出库的版本_Windows下PaddleOCR在C++中的推理部署(CPU版本)CMake + VS2017

PaddleOCR是一个与OCR相关的开源项目,不仅支持超轻量级中文OCR预测模型,总模型仅8.6M(单模型支持中英文数字组合识别、竖排文本识别、长文本识别,其中检测模型DB(4.1M)+识别模型CRNN(4.5M)),而且提供多种文本检测训练算法(EAST、DB)和多种文本识别训练算法(Rosetta、CRNN、STAR-Net、RARE)。本文将教大家通过PaddleOCR(CPU版...

weixin_31304817的博客 1917

使用gulp插件进行跨越访问

在进行前端项目的开发中, 如果要进行跨域访问, 而不想在本地配置nginx 时, 可以使用gulp 的gulp-connect 和gulp-connect-proxy 两个插件, 完成本地的跨域访问: var Proxy = require('gulp-connect-proxy'); var connect = require('gulp-connect');gulp.task("server",

robynhyb的博客 1013

STM32按键中断切换红绿蓝灯

通过以上步骤,我们实现了使用STM32的按键中断来切换红绿蓝LED灯的功能。在实际应用中,可能还需要考虑更多的细节,如按键消抖、更精确的延时实现等。但上述代码提供了一个基本的框架和思路,可以根据具体需求进行扩展和优化。希望这篇博客对您有所帮助!

2301_79865940的博客 952

Revit获取族预览图

最近做到和族库相关的功能需要获取到族文件的预览图,在RevitAPI中提供了ElementType.GetPreviewImage方法,但是前提就是你是在Revit里面操作的,并且已经拿到了Symbol对象实例,才可以调用此方法。然后在网上找了另外一种方式,通过读取Revit文件,在REVITPREVIEW4.0字段截取文件信息,将其转换为图片格式。 一、使用RevitAPI中ElementType.GetPreviewImage方法 [Autodesk.Revit.Attributes.Transac.

weixin_44631419的博客 1757

在gulp-server 中使用 http-proxy-middleware 配置代理跨域

简介 http-proxy-middleware 用于后台将请求转发给其它服务器,也就是我们平常说的跨域请求。例如当我们在开发项目的时候,使用gulp搭建本地服务器地址为http://127.0.0.1:8008/,使用 json-server 创建模拟数据的地址为http://localhost:3000/,用我们本地项目去请求数据的时候,因为端口号不同,涉及到了跨域。(项目开发完成,线上部署的...

蒲公英芽的博客 2550

VLA强化学习训练:π0到π0.5的技术优化与实现

视觉语言动作模型(VLA)作为多模态强化学习的重要分支,通过融合视觉、语言和环境状态数据实现智能决策。其核心技术在于多模态特征融合与动作空间设计,其中PPO算法因其出色的训练稳定性成为首选方案。在工程实践中,延迟补偿机制和课程学习设计能显著提升模型性能,如在四足机器人项目中使避障成功率提升47%。针对实际部署中的性能下降问题,需要特别关注硬件量化参数和控制频率一致性等关键因素。本文通过π0与π0.5版本的对比实验,详细解析了VLA模型在机械臂控制等具身智能场景中的优化路径。

weixin_29041767的博客 248

项目流程/gulp/前后端跨域

角色 1. 总监 决定项目使用哪种开发工具, 使用哪个框架开发 开发工具: gulp webpack(主流) 自动构建项目的快速开发工具 框架: jq bs vue angular react 2. 构建版本控制工具 git svn 3. 决定项目托管的地方 github 码云(gitee) 4. 创建码云的环境 5. 创建分支 默认直接创建好的 远程主分支(master) 创建多个...

LionKing0823的博客 514

CONN工具箱分析fmri数据笔记1

前言一、安装二、预处理三、去噪 前言 CONN:functional connectivity toolbox,是基于MATLAB和SPM的一个对功能磁共振数据进行分析的一个工具箱。可以进行以下分析: Seed-based correlations (SBC), ROI-to-ROI correlations, graph analyses, Independent component analyses(group-ICA), Masked (ICA) Amplitude of Low-Frequency.

weixin_52073820的博客 6587

实例详解C++程序的五大内存分区

本文详细讲述C++程序在运行时的5大内存分区。 下面使用日常开发中的编程实例,详细介绍一下这5个分区,以便大家能更深刻的理解这5大内存分区。 1、栈内存区 栈内存区是我们用的最多的分区,只要有函数的地方都会使用到这个分区。栈分区是用来存放函数参数及函数局部变量值的内存区,是由编译器在编译时自动分配和释放的。 函数中的参数与函数中的局部变量占用的内存是代码执行到函数(进入函数)是分配的,在............

dvlinker的技术专栏 1万+

gulp解决跨域的配置文件

//引入插件 var gulp = require('gulp'); // var Proxy = require('gulp-connect-proxy'); var connect = require('gulp-connect'); var proxy = require('http-proxy-middleware'); //使用connect启动一个Web服务器...

weixin_34233618的博客 641

机器人环境仿真软件:Webots_(9).路径规划与导航算法仿真实现

通过仿真,我们可以验证路径规划算法的有效性和鲁棒性,同时优化机器人在不同环境中的导航性能。动态窗口法是一种局部路径规划算法,通过在机器人当前速度空间中选择一个最优的速度窗口,使机器人在动态环境中安全地移动。算法的代价函数通常包括两个部分:从起始点到当前节点的实际代价(g-cost)和从当前节点到目标节点的估计代价(h-cost)。在World Editor中,选择“Insert”菜单,添加“Robot”对象,如“E-puck”或“Thymio”。Dijkstra算法的实现与A*算法类似,但不使用启发函数。

kkchenjj的博客 2006

Gulp自动化构建工具的简单使用

相关网站 gulp官方网址:http://gulpjs.com gulp中文网站:http://www.gulpjs.com.cn/ gulp插件地址:http://gulpjs.com/plugins gulp 官方API:https://github.com/gulpjs/gulp/blob/master/docs/API.md gulp 中文API:http://www.ydcss...

angyangan9569的博客 140

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Gulp的安装以及使用

Gulp的安装以及使用

Jinianq的博客 709

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

gulp的安装以及使用

gulp的安装以及使用

m0_70336694的博客 1797

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

web前端-Gulp入门

gulp: 前端自动化打包固件工具,gulp是基于流格式的一种打包构建工具,依赖node环境进行开发, 底层封装的内容就是node里面的读写文件。什么是流?流文件: 流一种文件传输的格式一段一段的文件传输流格式: 流从头到尾的一个过程需要从 源 开始一步一步经过加工,每一个步骤都需要依赖上一步的结果,最终给出一个完成的成品。gulp的安装是一个JavaScript相关的工具就是可以直接使用npm 进行安装需要安装在你的电脑环境里面, 一次安装多次使用。

yong_19930826的博客 601

gulp两种配置方式总结

c o n n e c t . s e r v e r ( r o o t : [ a p p . d e v P a t h ] , l i v e r e l o a d : t r u e , / / 自 动 加 载 p o r t : 1234 ) / / 自 动 打 开 浏 览 器 o p e n ( ′ h t t p : / / l o c a l h o s t : 123 4 ′ ) / / 实 现 自 动 编 译;nect.reload()) }̲) //以下任务会有变化 //…

2501_90229147的博客 919

【任务执行工具】gulp入门

gulp入门

menghen113的博客 817

带你一篇文章学会gulp

一、gulp是什么? 前端自动化打包构建工具; 打包:把文件压缩,整合,移动,混淆 基于流的打包构建工具; webpack:基于j's

qq_41629684的博客 551

gulp的4个API 让你成为gulp专家

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33726313的博客 98

gulp两种配置方式总结(1)

小编综合了阿里的面试题做了一份前端面试题PDF文档,里面有面试题的详细解析开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

2401_84413092的博客 861

gulp-简介

1 简述 gulp是一种构建工具,构建是什么意思呢?我感觉就是通过自己写一些脚本(gulpfile.js)让gulp工具运行,自动化的完成一些文件汇聚、CSS编译、植入链接等工作。 1.1 安装 安装方法还是很简单的,官网也有讲,出于完整性的考虑,还是罗列一下用到的命令。 sudo npm install --global gulp sudo npm install --s

m0_37978717的博客 2278

gulp快速入门总括篇

自动化工具gulp 当下的前端开发 不再是简单的使用HTML+CSS+js 这些技术构建网页应用 我们要提高效率,就必须减少重复的工作 使用less之类预处理的css coffeescript 提供开发阶段的便利,开发阶段更快捷 现在的开发行业优质的开发人员是不应该将精力放在这些重复性的工作上 gulp就是一种可以自动化完成我们开发过程中大量的重复工作 预处理语言的编译 js css ...

胡西风的博客 516
上一篇: 考研英语 - word-list-44
下一篇: 手写gulp脚本打开本地浏览器支持mac,windows
Wang's Blog
博客等级 码龄12年 7438粉丝 2421原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值