美文网首页
node搭建跨域

node搭建跨域

作者: 0安 | 来源:发表于2020-02-10 13:08 被阅读0次

有node环境,进入项目根目录     创建两个文件  proxy.js  main.js

npm install http-proxy --save-dev

var PORT = 3000;

var http = require('http');

var url = require('url');

var fs = require('fs');

var mine = require('./mine').types;

var path = require('path');

var httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer({

target: 'http://dev-web.xzjcloud.com/', //接口地址

// http://java.winfreeinfo.com/

// 下面的设置用于https

// ssl: {

//    key: fs.readFileSync('server_decrypt.key', 'utf8'),

//    cert: fs.readFileSync('server.crt', 'utf8')

// },

// secure: false

// '/api':{

// target: 'http://java.winfreeinfo.com/',

// },

// '/baidu':{

// target: 'http://www.baidu.com/',

// },

});

/****************************/

/****************************/

proxy.on('error', function(err, req, res) {

res.writeHead(500, {

'content-type': 'text/plain'

});

console.log(err);

res.end('Something went wrong. And we are reporting a custom error message.');

});

var server = http.createServer(function(request, response) {

/****************************8/

*

*

*

*/

var pathname = url.parse(request.url).pathname;

//var realPath = path.join("main-pages", pathname); // 指定根目录

var realPath = path.join("./", pathname);

console.log(pathname);

console.log(realPath);

var ext = path.extname(realPath);

ext = ext ? ext.slice(1) : 'unknown';

console.log("********" + pathname)

//判断如果是接口访问,则通过proxy转发

if(pathname.indexOf(".") < 0) {

proxy.web(request, response);

return;

}

// if(pathname.indexOf()!=undefined){

//      proxy.web(request, response);

//  return;

// }

fs.exists(realPath, function(exists) {

if(!exists) {

response.writeHead(404, {

'Content-Type': 'text/plain'

});

response.write("This request URL " + pathname + " was not found on this server.");

response.end();

} else {

fs.readFile(realPath, "binary", function(err, file) {

if(err) {

response.writeHead(500, {

'Content-Type': 'text/plain'

});

response.end(err);

} else {

var contentType = mine[ext] || "text/plain";

response.writeHead(200, {

'Content-Type': contentType

});

response.write(file, "binary");

response.end();

}

});

}

});

});

server.listen(PORT);

console.log("Server runing at port: " + PORT + ".");

这个是proxy.js  设置转发的js文件

以下为main.js是类型

exports.types = {

  "css": "text/css",

  "gif": "image/gif",

  "html": "text/html",

  "ico": "image/x-icon",

  "jpeg": "image/jpeg",

  "jpg": "image/jpeg",

  "js": "text/javascript",

  "json": "application/json",

  "pdf": "application/pdf",

  "png": "image/png",

  "svg": "image/svg+xml",

  "swf": "application/x-shockwave-flash",

  "tiff": "image/tiff",

  "txt": "text/plain",

  "wav": "audio/x-wav",

  "wma": "audio/x-ms-wma",

  "wmv": "video/x-ms-wmv",

  "xml": "text/xml",

  "woff": "application/x-woff",

  "woff2": "application/x-woff2",

  "tff": "application/x-font-truetype",

  "otf": "application/x-font-opentype",

  "eot": "application/vnd.ms-fontobject"

};

相关文章

  • node搭建跨域

    有node环境,进入项目根目录 创建两个文件 proxy.js main.js npm install http...

  • node跨域访问数据

    node返回数据时,设置下面头部跨域实现跨域

  • 前端请求

    node 简单跨域 前端简单请求

  • 关于请求的理解

    在node环境,不存在跨域问题, 在浏览器环境才存在跨域的问题. 在node环境,用不了axios , 在浏览器环...

  • 记录搭建Webpack自用框架所遇到的问题

    1.如何解决使用基于express框架搭建的node前端服务器调用接口时的跨域问题? 使用npm提供的 http-...

  • node+express(解决跨域)

    最近在研究node+express搭建简单的后台,但发现了跨域如下图: 因为我的后端地址的端口号为3000,前台启...

  • Node Js 跨域

    No 'Access-Control-Allow-Origin' header is present on the...

  • node 解决跨域

    解决跨域的方法有很多,慢慢总结,现在先介绍最常用的一种 这种的好处在于支持get,post等各种请求 具体原理可阅...

  • node vue 跨域

    小小兴奋下自己写的小项目终于过了跨域的这个坎。其实无外乎header加上各种允许,但是任就会有奇奇怪怪的问题出现。...

  • node跨域头

    Nodejs解决跨域请求 例

网友评论

      本文标题:node搭建跨域

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