node搭建跨域

有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"

};

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文件目录下创建 http.js为服务器设置文件 var PORT = 3000;// var http = req...
    程序猿吴彦祖阅读 350评论 0 0
  • 好开心!今天学会了搭建服务端。应对那些接口还未开发出来,需要调取数据的应用,总是写死在移动端的数据觉得很low,所...
    霸道总裁跟班阅读 551评论 0 0
  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 2,511评论 1 37
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,368评论 0 3
  • Node.js Stream(流) Stream 是一个抽象接口,Node 中有很多对象实现了这个接口。例如,对h...
    FTOLsXD阅读 622评论 0 2