Nodejs Agent solves cross-domain problem in development environment

Source: Internet
Author: User

A project that is separated from the front and back ends needs to address cross-domain issues in the development environment:

Reprint Link: https://www.cnblogs.com/ytu2010dt/p/5959899.html

1. Installing the Node environment

2. New JS file: Nodeproxy.js

Code:

"Use Strict"; Const Express= Require (' Express '); Const path= Require (' path '); Const app=Express (); Const request= Require (' request ')); //Configuring the static file Services middlewareLet Serverurl= ' http://220.231.2.29:7668 ';//Server AddressApp.use (Express.static (Path.join (__dirname, './'));//static resource index.html and node code in one directoryApp.use ('/',function(req, res) {Let URL= ServerURL +Req.url; Req.pipe (Request (URL)). pipe (res);}); App.listen (3000, ' 127.0.0.1 ',function() {//front-End Ajax address write Http://127.0.0.1:3000/Console.log (' server is running at Port 3000 ');});

3. Need to install Express and request:

NPM Install Express

NPM Install request

4. Run JS script node nodeproxy.js

node to do proxy forwarding request server, the project does not have to set up the front node service, you can solve the development environment of cross-domain problems;

Nodejs Agent solves cross-domain problem in development environment

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.