JS Learning 14 days----server-side Execution JS code

Source: Internet
Author: User

Server-Side Execution JS code

Saying,not currently used by clientsJScode is rare..becauseWebthe experience of the app is getting richer,Client withJSmore and more logic is being implemented.,The result is that some almost identical logic needs to be implemented in both the client and the server.,Daniel, of course, is not reconciled.!Fortunately,,we can do it on the server side .JSCode,who letJSholding a thigh ....

For example,now in the client useJSvalidation is already a standard .,He can effectively prevent users from submitting the wrong data under normal circumstances.,enhance the user experience.of course,server-side validation is also essential,because that 's what security is all about..Some Solutions,,provides a prioritized authentication type on the server side,the client then generatesJSCode,and assist with the server-side validation framework.This approach can be traced back toASPone of the controls on,but it's obviously going to be scalable .,Limitations on Flexibility,Therefore, they are more inclined to perform with server sideJSCode.

For example , to check if the user name is legitimate , We might write code like this :

var checkname=function (name) {return/^\w{3,10}$/.test (name);

}

This does not have any problem with client-side validation, and the server will have to use some javascript execution engine. On the .net platform There are for example a relatively new Ironjs project, this is a Roman based on style= font-family " javascript execution engine, which attaches great importance to performance, Judging from the results of the authors blog, even ahead of the speed of the v8 ironjs has not been fully implemented ecmascript 3.0

Jint is an older JavaScript execution engine on the . NET platform , so it is not very related to the DLR , so it may not be easy to IronPython,IronRuby and other languages to interoperate. It is not a problem to use it to execute some simple JavaScript scripts, such as the code above:

var jint = new Jint.jintengine (); Jint. Run (@ "var checkname = function (name) {return/^\w{3,10}$/.test (name);}"); Console.WriteLine (Jint. CallFunction ("CheckName", "JEFFZ")); Trueconsole.writeline (Jint. CallFunction ("CheckName", "Hello World"); False

Unfortunately, in practice,Jint does not support multi-threaded environments, which means that we cannot simultaneously invoke Jint 's callfunction method under multiple threads. But if you Run the JavaScript code again every time, you will have a lot more performance overhead. In fact, it is not difficult to solve this problem, the construction of an object pool,. NET 4 provides a parallel container (such as concurrentstack, Concurrentqueue), implementing a Simple object pool is effortless.

This is a much better performance for Jurassic, a JavaScript execution engine built on . NET 4.0 :

var engine = new Jurassic.scriptengine (); engine. Evaluate (@ "var checkname = function (name) {return/^\w{3,10}$/.test (name);}"); Console.WriteLine (engine. Callglobalfunction<bool> ("CheckName", "JEFFZ")); Console.WriteLine (engine. Callglobalfunction<bool> ("CheckName", "Hello World");

In addition, from Benchmark to see ,Jurassic performance is also higher than Jint , but still far behind V8 and even the JavaScript engine in IE 8 . It also provides a Silverlight-based console that you can play with in your browser.

It's surprising thatJintand theJurassicas aJavaScriptThe execution engine has some serious problems that are not working correctlyShowdown.js (JavaScriptimplementation ofMarkdownConverters )--Although I did not findShowdown.jsThere is too much complexity in the content, basically is some string operation it. Originally I wanted to use them inMono, there is no further attempt to do so. However, after a simple experiment,Jurassicseems to have usedMono 2.8interfaces that are not yet supported, it is possible that onlyJurassicproblems in the console.

Interestingly, the most reliable JavaScript execution engine under the. NET platform is actually Rhino JavaScript, the most recent release Year 3 months, but the implementation is very complete. To say the disadvantage, it may be more cumbersome to use, and, this is a Java project.

Well, I'm not kidding, we can use Rhino JavaScriptentirely under the . NET Platform :

var cx = Context.enter (); try{var scope = cx.initstandardobjects ();     Cx.evaluatestring (Scope, @ "var checkname = function (name) {return/^\w{3,10}$/.test (name);}", "Checkname.js", 1, NULL);     var func = (Function) scope.get ("CheckName", scope);    Console.WriteLine (Context.tostring (Func.call (CX, scope, scope, "JEFFZ"))); Console.WriteLine (Context.tostring (Func.call (CX, scope, scope, "Hello World");} finally {context.exit ();}

Because we have ikvm.net.Monowait. NETThere's a lot of treasure in the open source community, and it depends on how much you can use. I useIkvmcputJs.jartranslates toRhinoJs.dllafter that it can be used directly, the effect is very good, the debugging is also very good support (ifJavaScriptAn error occurred while executing, theVSwill take you directly to the wrong line). Performance is also relatively satisfactory, in myMac OSXinstalled on theUbuntu Server 10.10virtual machine, single-threaded conversion and filtering on the most recent blog3800comment, about time-consuming -seconds. At trialHostIt's still open with aWindows 7virtual machines, as well as a large number of browsers and other applications, are not very idle.

As you may know, my blog is currently based onMono 2.6, where the more distinctive feature is the comment function, which I useMarkdowntags, and provides real-time preview functionality, which naturally needs to be explained in the clientMarkdowntags, and filter them. At the moment, I'm still using the serverC #implementation ofMarkdownConverters and filter logic, but in some special cases the results will vary and two sets of code need to be maintained. Soon, I will upgrade my blog toASP. 4.0andMono 2.8(C # 4.0of theDynamicfeature is really convenient in some cases) and is used on the server sideikvm.net + Rhino JavaScriptexecute the same conversion code. The effect is still very satisfying.

It is worth mentioning that in fact, there is a DLR - based JavaScript execution engine on the . NET platform, which is for remobjects Script for. NET, also allegedly supports mono. It's a pity it's not an open source product (but it's a source code), and the licensing agreement requires us to install code on up to 5 machines, and only for our own use, so I don't have much attention.

JS The first part of the finished , Originally the second part still want to follow the instructions under the book down , found no meaning .

I think this is writing about the design pattern of things , or that sentence , Knock Code is not computer technology , to master a bit of the original rational things and thinking things .

Big 31 years not ready to learn the code , Learn the principle , Learn the idea !

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS Learning 14 days----server-side Execution JS code

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.