Home Web Front-end JS Tutorial Use node local proxy to request json file and return interface data

Use node local proxy to request json file and return interface data

Jul 16, 2018 pm 01:49 PM
javascript

In the normal development process, the front and back ends are often developed in parallel, and the backend interface cannot be called temporarily. At this time, we may often use local pseudo data, or use mock data. I mostly used this method in the past, but recently When I came to the new company, I found that this is using the node local proxy method to make interface simulation calls, and then the data is read and returned through the local json file. I personally think that this method can best reflect the many logics in the business code execution process, so I will briefly Do some research and record it. Of course, the premise is that the back-end and the front-end have a good interface communication method. The back-end has already given the interface name and return structure fields, so that after the back-end interface is written, it can be debugged directly without modification.
This demo uses the create-react-app scaffolding to initialize the project, uses antd-mobile for component display, uses node's express to build the local environment, and superagent to perform front-end and back-end requests. Since node execution file modifications require a restart, nodemon is used here. Start the node. When the node execution file is modified, the application background service will be automatically restarted.
The logical structure is very simple, and the node knowledge used is very little. Basically, you can directly develop the agent after reading the configuration once and using it. The components are mainly for display, so you don’t need to pay too much attention to some business details. The main purpose here is to show you how to make local proxy requests.
First of all, project structure:

Use node local proxy to request json file and return interface data

The src folder is the business code. This is not the point. app.js is the node execution file entry; the router.js file executes node reading. The method of returning data from the local josn file is implemented; config.js is some configuration files for proxying; proxy-confit.js is the proxy logic of the local proxy; and then the above folder proxy_data is the prepared local json file, which is used when calling the interface. A process in which node calls a local json file and then reads the file and returns the data.
The first thing to note is that package.json plus proxy configuration

Use node local proxy to request json file and return interface data

At present, it seems that this configuration is only initialized for create-react-app. The project works and its function is to modify the requested path to the proxy path. The host and port here need to correspond to the host port configured below.

详细解释一下:
app.js

var express = require('express');
var bodyParser = require('body-parser');
var router = require('./router');// 引入router
var config = require('./config');// 引入配置
var app = express();
app.use(router)// 注意执行
app.use(bodyParser.json())// 注意加上,否则返回的是数据流,不是json
app.listen(config.port, function () {// 启动应用
    console.log('server is run on ' + config.port);
})

config.js代理配置,这里目前只有host和port根据项目需求自己加上即可。
var config = {
    host: 'localhost',
    port: 5002,
}
//这里面最重要的在于host/port其他可以根据项目需要加进去,
module.exports = config;

router.js //详细的代理和读取文件逻辑
var express = require('express');
var fs = require('fs');
var proxyConfig = require('./proxy_config.js');// 引入代理逻辑
var router = express.Router();//注意执行
/*
 * RESTful 路由
 */
//router.get('/token', proxy.token);

// 下面文件执行逻辑在于当本地请求有符合proxy_config里面配置的正则,就会被代理到本地并且读取本地对        应json文件返回相应json数据
for(var i=0; i<proxyconfig.length module.exports proxy_config.js json><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn//upload/image/664/157/318/1531719633585316.png" class="lazy" title="1531719633585316.png" alt="Use node local proxy to request json file and return interface data"></p><p>See the github address for the detailed code. After downloading, execute install and then start the nodemon app to start express and then open npm run start to start the application. <br> In addition to the content of the proxy request, this demo is also a complete small demo of react. It uses antd-mobie for component development, and react-loadable based on the router page level performs on-demand loading and data transfer between parent and child components. and communication, simple life cycle presentation and component state data modification. </p><p>Github address: https://github.com/nextisleo/...<br>I will try to add redux later and use a small project to fully understand and develop react. </p><p>Related recommendations: </p><p><a title="iNotify.js2如何做出浏览器title的一些功能" href="http://www.php.cn/js-tutorial-406592.html" target="_blank">iNotify.js2 How to make some functions of browser title</a></p><p class="mt20 ad-detail-mm hidden-xs"><a title="如何通过Vue.js使用Font Awesome实现小图标" href="http://www.php.cn/js-tutorial-406567.html" target="_blank">How to use Font Awesome through Vue.js Implementing small icons</a><br></p><p class="mt20 ad-detail-mm hidden-xs"><a title="响应式React Native Echarts组件的介绍" href="http://www.php.cn/js-tutorial-406517.html" target="_blank">Introduction to responsive React Native Echarts components</a><br></p></proxyconfig.length>
Copy after login

The above is the detailed content of Use node local proxy to request json file and return interface data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles