JS Learning 14 days----Server side run JS code

Source: Internet
Author: User

Server-side run JS code

Saying,not used by the client todayJScode is rare..becauseWebthe experience of the app is getting richer,Client UseJSmore and more logic is being implemented.,The result is that some almost identical logic needs to be implemented on both the client and the service side .,Daniel, of course, is not reconciled.!Fortunately,,we can run on the server sideJSCode,who letJSholding a thigh ....

Like what,now in 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 an essential,because that 's what security is all about..some ways to solve,,provides a prioritized authentication type on the server side,then the client generatesJSCode,and assists with the server-side validation framework.This approach can be traced back to theASPone of the controls on,but it's obviously going to be scalable .,Limitations on Flexibility,therefore are more inclined to run with server sideJSCode.

For example , to check if username is legal , We might write code like this :

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

}

This is in the client verification nature no matter what problem, the server side with some JavaScript to run the engine. There are, for example, newer IRONJS projects on the . NET Platform, a DLR - based JavaScript runtime engine that attaches great importance to performance. Judging from the results of the author's blog. Even take the lead in the V8of Speed.

Unfortunately, Ironjs has not yet fully implemented ECMAScript 3.0. Some important features are missing. such as the normal form.

Jint is an earlier JavaScript run 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 are interoperable. Using it to run some simple JavaScript scripts is not a problem, like 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 practical use,Jint does not support multithreaded environments. That is, we cannot invoke jint 's callfunction method at the same time under multiple threads , but let's say Run it again every time. JavaScript code can also lead to more performance overhead. In fact, it is not difficult to solve the problem, the construction of an object pool can be,. NET 4 provides a parallel container (such as concurrentstack, Concurrentqueue), implementing a Simple object pool is effortless.

In this respect Jurassic performance is much better. This is a JavaScript run 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 see , jurassic performance is also greater than jint v8 ie 8 javascript engine. And. It also provides a silverlight console, You can play with it in your browser.

The

is surprising. jint jurassic as javascript The execution engine has some serious problems, which is that the showdown.js is not executed correctly ( javascript implementation of the markdown converter) -- Although I did not find the showdown.js

Originally I wanted to use them in mono jurassic seems to use the mono 2.8 Span style= "Font-family:times New Roman" >jurassic problems in the console.

Interestingly, the most reliable JavaScript running engine under the. NET Platform is Rhino JavaScript, and a recent announcement is Year 3 months, just achieve very complete. To say the disadvantage, it may be the use of more trouble, and. This is a Java project.

Well, I'm not kidding, we're completely able to use Rhino JavaScriptunder 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. Mono wait . NET There's a lot of treasure in the open source community. It depends on how much you can use.

I use ikvmc js.jar converted to rhinojs.dll javascript There was an error running. The vs

Performance is also a pleasant one. In my mac osx installed ubuntu server 10.10 virtual machine. Single-threaded conversion and filtering on blogs recent 3800 comment, Approximately time-consuming 20 seconds. At the time of the experiment host windows 7 virtual machines, as well as a large number of browsers and other applications, are not very spare.

As you may know, my blog is now based on mono 2.6 markdown markdown tags , and filter.

Right now. I also used the server c# implemented markdown converter and filter logic, but in some special cases the results will vary, and need to maintain two sets of code. Soon after. I will upgrade the blog to asp.net 4.0 and mono 2.8 ( Span style= "Font-family:times New Roman" >c# 4.0 dynamic features are indeed more convenient in some cases. and use the ikvm.net + rhino javascript on the server side run the same conversion code. The effect is still very pleasant to see.

It is worth mentioning that, in fact, another DLR - based JavaScript run engine on the . NET platform is for remobjects Script for. NET, also allegedly supports mono. It is only a pity that it is not an open source product (it only exposes the source code), and the licensing agreement requires us to install it on up to 5 machines. And only for our own use, so I have no concern for it too much.

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 patterns of things , or that sentence , writing procedures is not computer technology , to master a little of the original rational things and thinking things .

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

JS Learning 14 days----Server side run 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.