On the use of node.js

  node.js, question

The question asked is relatively small white, and I hope you can be patient and answer it.
Because the front end often interacts with the back end, and I don’t know much about the back end, I can’t do it most of the time, so I can’t even test the usage effect of ajax ().
Then I heard that node.js can be made, but I don’t know it at all. I went to the official website to learn about it. Baidu only said that node.js can be highly concurrent. I don’t know anything about server-side js, its details, its uses, how to use it.
Still hope to use the predecessors of node.js to point out a clear path! ! ! ! !

These things can’t be learned at once. If you don’t understand them before, you won’t understand them at once. Hold your horses and take your time!
If you want to see the effect of ajax execution, I’ll give you a small node program to see what the server receives when ajax requests are made.

/*
 * Author: ikether
 * Email:  ikether@126.com
 *
 */
 
 var http = require('http');
 
 
 var server = http.createServer();
 var count = 0;
 
 server.on('request', function(req, res) {
 req.body   = [];
 var length = 0;
 
 req.on('data', function(chunk) {
 req.body.push(chunk);
 Length plus = chunk.length;
 });
 
 req.on('end', function() {
 req.body = Buffer.concat(req.body, length);
 
 Log ('\ n [',countplus,']',' [',newdate (),'] http', req.method, req.url);
 console.log('-------------------------- header -----------------------------');
 for (k in req.headers) {
 Process.stdout.write('-' plus k plus':' plus req.headers[k] plus' \n');
 bracket
 console.log('--------------------------- body -------------------------------');
 console.log(req.body.toString('utf8'));
 res.setHeader('Access-Control-Allow-Origin', '*');
 res.end('OK');
 });
 
 req.on('error', function(err) {
 console.log(err);
 });
 });
 
 server.on('error', function(err) {
 console.log('error:', err);
 });
 
 server.listen(8123);

Save the above code as a raw.js file.

  • Open terminal input under unix-like operating systemnode raw.jsEnter to start the service.

  • Under Windows, find Node’s console program in the start menu, open it, and enter cd into the directory where raw.js files are stored.node raw.jsEnter to start the service.

Examples of corresponding web pages:

<!  DOCTYPE html>
 <html>
 <head>
 <meta charset='utf-8'/>
 <title>test ajax</title>
 </head>
 <body>
 <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
 <script>
 (function($) {
 console.log('hello');
 $.post('http://127.0.0.1:8123/', {abc: 'foo', age: 22}, function(data, status) {
 console.log(data);
 });
 })(jQuery);
 </script>
 </body>
 </html>

Save the above HTML code as raw.html, double-click to open the browser for execution, and you will see the following similar output in the console:

[ 6 ] [ Tue Jan 19 2016 12:46:09 GMT加0800 (CST) ] HTTP POST /
-------------------------- header -----------------------------
- host: 127.0.0.1:8123
- user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0
- accept: */*
- accept-language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
- accept-encoding: gzip, deflate
- content-type: application/x-www-form-urlencoded; charset=UTF-8
- content-length: 14
- origin: null
- connection: keep-alive
--------------------------- body -------------------------------
abc=foo&age=22

Is browser execution$.post('http://127.0.0.1:8123, {abc: 'foo' ....The actual request when waiting for code