Setting up the Http-proxy proxy locally (front-end detach)

Source: Internet
Author: User
Tags readfile

1, the use Package.json installs the Nodejs, creates the Package.json file: The content is as follows

"Dependencies": {
"Http-proxy": "^1.16.2"
}

2. Create index.js file Under current directory: content as follows
var PORT = 3000;
var ProjectPath = '/'. /project '; Local Project Path

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://192.168.12.220:8080/',//interface address
The following settings are used for HTTPS
SSL: {
Key:fs.readFileSync (' Server_decrypt.key ', ' UTF8 '),
Cert:fs.readFileSync (' server.crt ', ' UTF8 ')
// },
Secure:false
});

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) {
var pathname = Url.parse (request.url). Pathname;
var Realpath = Path.join ("main-pages", pathname); Specify the root directory
Console.log (pathname);
var Realpath = Path.join (".") +projectpath+ '/', pathname);
var ext = path.extname (Realpath);
Ext = ext? Ext.slice (1): ' Unknown ';

Determine if the interface is accessed by proxy forwarding
if (Pathname.indexof ("/deviceservice") >-1) {
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 + "is not found on the 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 + ".");

3, as index.js the second line is configured: Create a project folder in the top directory of the current directory projects
put index.html test page

<! DOCTYPE html>
<meta charset= "UTF-8"/>
<title>1111</title>
<script>
function ReadFile (a,back) {
SetTimeout (function () {
Back (a);
},1000);
}

Const THUNK = function (fn) {
return function (.... args) {
return function (callback) {
Return Fn.call (this, .... args, callback);
}
};
};
var tf = Thunk (readFile);

var GG = {
*g () {
var f1 = yield tf (' FileA ');
Console.log ('---->> ', F1);
var F2 = yield tf (' Fileb ');
Console.log ('---->> ', F2);
if (f2== "Fileb")
Return
// ...
var fn = yield tf (' filen ');
Console.log ('---->> ', FN);
}
}
var g = function* () {
var f1 = yield tf (' FileA ');
Console.log ('---->> ', F1);
var F2 = yield tf (' Fileb ');
Console.log ('---->> ', F2);
if (f2== "Fileb")
Return
// ...
var fn = yield tf (' filen ');
Console.log ('---->> ', FN);
}

Const RUN_THUNK = function (fn) {
var gen = fn ();

function next (err, data) {
var result = Gen.next (err);
if (Result.done) return;
Result.value (next);
}
Next ();
}

function * A () {
Yield 1;
Yield 2;
}

var init = function () {
Run_thunk (GG.G);
var m = setTimeout (function () {
Alert (' hell ');
},5000);

SetTimeout (function () {
Cleartimeout (m);
Alert (' 111 ');
},1000);
var i = a ();
Console.log (I.next ());
Console.log (I.next ());
Console.log (I.next ());
}
Init ();
</script>
<body>
1111
</body>

4. CNPM Install

5, Node Index.js Open 3000 Service

6, Note: mine.js content:

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"
};

Project Structure:-----nodeserver

|----Package.json

|----index.js

|----mine.js

|----node_modules

-----Project

|----index.html

Setting up the Http-proxy proxy locally (front-end detach)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.