This article mainly introduces the use of Node. the js + Koa framework implements the frontend-to-backend interaction method. This article describes the methods in detail and has some reference value for everyone. If you need them, let's study them together.
Preface
For a front-end engineer, not only the pre-meeting content, but also the backend technology needs to be mastered. Today, I will use a case to describe how the front-end interacts with the back-end data.
Koa is built by Express's original team and is committed to becoming a smaller, more expressive, and more robust Web framework. Using koa to write web applications and combining different generators can avoid repeated and tedious callback function nesting and greatly improve error processing efficiency. Koa does not bind any middleware in the kernel method. It only provides a lightweight and elegant function library, making it easy to write Web applications.
Preparations
First, we need to deploy the node. js environment for our servers. Here we use a local node. js server in windows for demonstration.
For details about how to deploy the Node. js environment, refer to the document "detailed steps for installing Node. js in Windows.
If you are not familiar with the Koa framework, refer to this article: getting started with the Node. js Koa framework and MySQL operation guide.
The method is as follows:
After the environment is deployed, we need to create a project directory and install the Koa framework and some dependent libraries in the directory through npm.
Is the directory structure after I complete it.
Next, create an app. js file to set the route used to access the server. The Code is as follows:
Var koa = require ('koa'); var controller = require ('koa-route '); // you need to use npm to add this dependency var app = koa (); var service = require ('. /service/WebAppService. js'); // reference WebAppService. js/* set route */app. use (controller. get ('/ajax/search', function * () {this. set ('cache-control', 'no-cache'); this. set ('access-Control-Allow-origin', '*'); var querystring = require ('querystring'); var params = querystring. parse (this. req. _ parsedUrl. query); var key = params. key; var start = params. start; var end = params. end; this. body = yield service. get_search_data (key, start, end) ;}); app. listen (3001); console. log ('koa server is started ');
By default, there is no koa-route dependency in the node_modules folder. You need to install it through npm koa-route.
Then we need to create a WebAppService. js file under the service directory to request the interface. The Code is as follows:
Var fs = require ('fs'); exports. get_search_data = function (key, start, end) {return function (cb) {var http = require ('http'); var qs = require ('querystring '); var data = {key: key, start: start, end: end};/* request the train ticket query interface in the MobAPI */var content = qs. stringify (data); var http_request = {hostname: 'apicloud .mob.com ', port: 80, path:'/train/tickets/queryByStationToStation? '+ Content, method: 'get'}; var req = http. request (http_request, function (response) {var body = ''; response. setEncoding ('utf-8'); response. on ('data', function (chunk) {body + = chunk;}); response. on ('end', function () {cb (null, body) ;}); req. end ();}}
In this way, an interface is actually transferred. We can not only request a local interface but also a third-party interface to avoid the browser blocking the request during cross-origin requests.
Next, run the command to start the service, and enter node app. js on the terminal.
For more articles on XNode. js + Koa framework for front-end and back-end interaction, refer to PHP!