Web front end efficiency improvement-nginx+nodejs building local ecology

Source: Internet
Author: User
Tags readfile

1. Causes

The project was written as a web system with a preference for background management, using the angular framework, which relies on the interface format of the backend when binding data.

Before is the background to write the interface, I am binding, before this some logic is not able to do, and sometimes the background interface to slow, it is necessary to bind false data to write dead in JS inside,

Feel very passive, background interface, server out of something wrong, my progress will be delayed, return the format is not friendly, or return the format and pass the format is not the same, I also want to go to format.

People Pat Butt said interface written well, go home to rest, I will work overtime to write code.

2. Solution Ideas

Return format and accepted format, form submitted as an example

  

<input ng-model= "Data.type" type= "text" >
<input ng-model= "Data.id" type= "text" >

If you save the modified information in the background field becomes data_id data_type, you need to do a mapping. The nature of my expectation is to get and submit the same format, so that you can maximize the performance of MVVC

The first step is to define the format and URL returned by the front-end, of course, to find the backend engineer to confirm

The second step is to build the local server with Nodejs

Converts the URL to a file name, reads the TXT format file, and does not create it automatically to store the data to be returned.

The third step is to use Nginx for request forwarding.

Nginx used to do a static server, he can be configured to intercept the specified URL, and the request L forwarded to the specified address, so that we can open two ports, a server to connect to the background developer, one to

Connect your own set of NODEJS services

3. Specific steps

Install Nginx, and use Nginx to do the request forwarding please Baidu

Install Nodejs, WS module (websocket request, crossing net), run the file, please own Baidu

Nodejs code that executes the script

  

var http = require (' http ');

var url = require (' URL ');

var readFile = require ("FS");

Http.createserver (function (request,response) {

var params = Url.parse (request.url,true). query;

var textname = Request.url.slice (1,request.url.length). Replace (/\//g, "_");//convert URL to file name

var path = textname+ ". txt";

Readfile.exists (path, function (exists) {

if (!exists) {//does not exist then create

Readfile.writefile (path, "{\ r \ n}", function (Err) {

if (err) {

return Console.log (ERR);

}

Responsejson (Path,response);

});

}else{

Responsejson (Path,response);

}

});

})

. Listen (8888);

Console.log ("Am-server is running");

function Responsejson (path,response) {

var data =json.parse (Readfile.readfilesync (Path, "Utf-8"));//Read file contents and convert to Object

Response.writehead (200,{

' Content-type ': "Application/json"

})

var responsedata = {

Data

code:200

}

Response.End (Json.stringify (responsedata));

}

* The above program is supported by our project team back-end engineers and project leaders. Change the port to get the data, testing, development does not rely on the background.

var http = require (' http ');
var url = require (' URL ');
var readFile = require ("FS");
Http.createserver (function (request,response) {
var params = Url.parse (request.url,true). query;
var textname = Request.url.slice (1,request.url.length). Replace (/\//g, "_");
var path = textname+ ". txt";
Console.log (PATH)
Readfile.exists (path, function (exists) {
if (!exists) {
Readfile.writefile (path, "{\ r \ n}", function (Err) {
if (err) {
return Console.log (ERR);
}
Responsejson (Path,response);
});
}else{
Responsejson (Path,response);
}
});


})
. Listen (8888);
Console.log ("Am-server is running");
function Responsejson (path,response) {
var data =json.parse (Readfile.readfilesync (Path, "Utf-8"));
Response.writehead (200,{
' Content-type ': "Application/json"
})
var responsedata = {
Data
code:200
}
Response.End (Json.stringify (responsedata));
}

Web front end efficiency improvement-nginx+nodejs building local ecology

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.