Areas of expertise in Node. js are:
- Does not require many operations
- High throughput requirements
- Lightweight and fast incoming messages
- Low outgoing message and fast request
The online examples are socket. io. I have been wondering where I can use it? Based on the advantages of node. js (fields of expertise), I came up with this application scenario:
In the autocomplete Application Scenario of jQuery UI, the required data is fragmented but large.
The structure is as follows:
- Use ASP. NET MVC4 to create a web site. Compile jQuery UI autocomplete code here
- Use WebMatrix to write Node. js Code (mainly express-based routing)
Because these two sites are independent, cross-domain issues are involved. Use JSONP to solve the problem. (In fact, You must modify the corresponding code in both js Code calls and node. js ).
Post ASP. NET MVC4 code first
dataType: ); }, error:
Then, create the Node. js program and check the new Node. js Dialog in WebMatrix:
After the new file is created, you will see a bunch of files have been created. first look at the server. js entry file and then look at the route file:
express = require('express' , http = require('http' , path = require('path' , routes4users = require('./routes/users' app ='port', process.env.PORT || 3000'views', __dirname + '/views' app.set('view engine', 'jade' app.use(express.favicon()); app.use(express.logger('dev''development', '/users/:name'
'port'), "Express server listening on port " + app.get('port'
Look at the users. js file:
.UserName = .Age = -1 users = ( i = 0; i < 10; i++ u== "aaron" +== searchKey = result = ( i = 0; i < users.length; i++ (users[i].UserName.indexOf(searchKey) >= 0 callbackFunctionName = res.setHeader('Content-Type', 'application/javascript; charset=utf-8' str == callbackFunctionName+"(" + str + ")" res.send(str);}
Run: