NodeAsp is a set of Classic ASP framework, which draws on the modular idea of NodeJS, allowing you to happily write ASP programs using new concepts.
NodeAsp uses require that follows the CommonJS specification and is fully compatible with the NodeJS module loading method, allowing you to directly use more than 50% of NodeJS modules. All modules that have nothing to do with the NodeJS running environment and ES5-ES6 specific objects can be used directly. Such a huge module resource library has not been available in any ASP framework in the past.
NodeAsp is a unique and innovative framework in the ASP field. Her emergence has changed the traditional ASP writing model, allowing you to complete front-end and back-end development at the same time only by knowing js, and eliminating the tedious process of deploying NodeJS servers.
NodeAsp is here as the terminator in the ASP field.
NodeAsp: http://nodeasp.com
Module download: http://nap.webkits.cn
This framework only runs on the WIN platform IIS. Usually an ordinary ASP virtual host can run this framework.
default.asp
<!-- #include file="NodeAsp.asp" -->
<%
require( ' ./index.js');
%>index.js
var http = require ( 'http' ) ;
http . createServer ( function ( req , res ) {
res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
res . end ( 'Hello Worldn' ) ;
} ) ; Step 1: Download NodeAsp.
Step 2: Reference NodeAsp.
<!--#include file="NodeAsp.min.asp" -->Step 3: Use NodeAsp.
< %
var version = process.version;
Response.Write(version);
% > These objects are available in all modules. Some objects are not actually in the global scope, but only in the module scope - this situation is specifically pointed out in the following documentation.
In browsers, the top-level scope is the global scope. This means that in the browser, var something will declare a global variable if it is currently in the global scope. It's different in NodeAsp. The top-level scope is not the global scope, var something in the NodeAsp module only belongs to that module.
Output some information about the running environment.
In NodeAsp, the main purpose of process existence is to be compatible with certain NodeJS modules, which are usually not used.
Used to print standard output and standard error.
See the控制台section below for details.
We have introduced the buffer module to be compatible with NodeJS's Buffer. Please note that the buffer module must be included under node_modules to use Buffer.
If you forget to put the buffer module under node_modules, this will not affect the normal operation of the program. An error will be thrown only when you use Buffer.
Import modules. A little different from NodeJS is that because IIS can only directly execute ASP files instead of JS files, require can also be used to require a module in ASP code. Similar to executing node test.js on the command line.
If you want to know the real file path when calling the require() method to load the module, you can use the require.resolve() method to get it.
See模块section below for details.
The file path of the currently executing code file. This is the parsed absolute path to the code file.
For example: execute C:websitesnodeaspindex.asp
// module.js
Response . Write ( __filename ) ;
// C:websitesnodeaspmodule.js
// index.asp
require ( './module' ) ;
Response . Write ( __filename ) ;
// C:websitesnodeaspindex.aspThe directory name of the directory where the script is currently executed.
A reference to the current module. In particular, module.exports and exports point to the same object. Module is actually not global but local to each module.
See模块section below for details.
A reference to the module.exports object, which is shared by all instances of the current module and can be accessed through require(). Details on when to use exports and when to use module.exports can be found in the module system documentation. Exports are actually not global but local to each module.
See模块section below for details.
The timer function includes the following four functions. Since ASP is single-threaded, the following functions are actually incompatible.
setTimeout(cb, ms)
clearTimeout(t)
setInterval(cb, ms)
clearInterval(t)
In order to better debug ASP programs, we implemented a command line debugging tool similar to NodeJS/Chrome browser. Download address: https://github.com/Spikef/NodeAsp-Console
NOTE: IIS7.5 and .NET4 are required to run, and other environments have not been tested.
Open CMD with administrator rights and use the REGASM command to register componentTerminal.dll, where REGASM is located at C:WindowsMicrosoft.NETFrameworkv4.0.30319 (the specific location is related to the version number).
C:WindowsMicrosoft.NETFrameworkv4.0.30319REGASM D:componentTerminal.dll /codebase
For 32-bit systems, find the following registry location:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftInternet ExplorerMAINFeatureControlFEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701
For 64-bit systems, find the following registry location:
HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftInternet ExplorerMAINFeatureControlFEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701
Select or create a new item FEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701 , and then create a new DWORD value:
Name: w3wp.exe Value: 1
Double-click Console.exe to open the NodeAsp debugging command line.
Enter the following code in default.asp, and then access default.asp through the browser. Next, you can see the results in Console.exe .
var a = { name : "nodeasp" , value : true }
// 在console中将输出
//{
// name: "nodeasp",
// value: true
//}Output to Console, using default colors.
var a = { name : "NodeAsp" , version : "0.0.1" } ;
console . log ( a ) ; For output to the Console, use green.
For output to the Console, use red. In particular, if the output is an Error object, the complete error message will be displayed.
For output to the Console, use yellow.
Use label to specify the name and start a timer to calculate the time required for the operation.
Output the time required for an operation.
Example:
console . time ( '100-elements' ) ;
for ( var i = 0 ; i < 100 ; i ++ ) {
;
}
console . timeEnd ( '100-elements' ) ;
// prints 100-elements: 262msYou can enter the following commands in the console.
cls/clear: Clear debugging information and cannot be restored.
about: Display about information.
copylast: Copy the previous output message.
copyall: Copy all output information.
NodeAsp has a module loading system that is almost identical to NodeJS, which ensures that NodeAsp can directly use a large number of NodeJS modules.
The core of NodeAsp contains almost no functions required for website development, and all functions are extended through modules. You can find the function modules you need through NodeAsp's module center or NPM.
In NodeAsp, files and modules have a one-to-one correspondence. Below is an example of foo.js loading circle.js in the same directory.
The contents of foo.js
var circle = require ( './circle.js' ) ;
console . log ( 'The area of a circle of radius 4 is '
+ circle . area ( 4 ) ) ;Contents of circle.js:
var PI = Math . PI ;
exports . area = function ( r ) {
return PI * r * r ;
} ;
exports . circumference = function ( r ) {
return 2 * PI * r ;
} ;The circle.js module outputs two functions, area() and circumference(). To export an object, just add it to the special object exports.
Note that exports is a reference to module.exports, just for convenience. When you want to export a single item such as a constructor, you need to use module.exports.
// 正确输出构造函数
module . exports = MyConstructor ;Local variables within a module are private. In this example, the variable PI is private to circle.js.
Consider this situation:
a.js
console . log ( 'a starting' ) ;
exports . done = false ;
var b = require ( './b.js' ) ;
console . log ( 'in a, b.done = %j' , b . done ) ;
exports . done = true ;
console . log ( 'a done' ) ;b.js
console . log ( 'b starting' ) ;
exports . done = false ;
var a = require ( './a.js' ) ;
console . log ( 'in b, a.done = %j' , a . done ) ;
exports . done = true ;
console . log ( 'b done' ) ;main.js
console . log ( 'main starting' ) ;
var a = require ( './a.js' ) ;
var b = require ( './b.js' ) ;
console . log ( 'in main, a.done=%j, b.done=%j' , a . done , b . done ) ;First, main.js loads a.js, and then a.js loads b.js. At this time, b.js will try to load a.js. In order to prevent infinite loops, a.js will return an unfinished copy to b.js. Then b.js will stop loading and return its exports object to the a.js module.
In this way, main.js has loaded both modules. The output of this program is as follows:
main starting
a starting
b starting
in b , a . done = false
b done
in a , b . done = true
a done
in main , a . done = true , b . done = trueLike NodeJS, usually cyclically dependent modules will not lead to an infinite loop. However, if you directly execute the methods of other modules when the module is loaded, you will be prompted that the corresponding method cannot be found, so you should avoid this situation.
// a.js
var b = require ( './b.js' ) ;
exports . add = function ( m , n ) {
console . info ( m + n ) ;
} ;
// b.js
var a = require ( './a' ) ;
var m = 101 , n = 102 ;
exports . result = function ( ) {
a . add ( m , n ) ; // 此处没有问题
} ;
a . add ( m , n ) ; // 此处会报错,找不到a.add方法 If the file name is not found, NodeAsp will add the .js and .json suffixes and try to load again.
.js will be parsed as a Javascript plain text file, and .json will be parsed as a plain text file in JSON format.
If the module is prefixed with '/', it represents an absolute path. For example, require('/home/marco/foo.js') loads the file /home/marco/foo.js.
If the module is prefixed with './', the path is relative to the file calling require(). In other words, circle.js must be in the same directory as foo.js for require('./circle') to find it.
When a file is not pointed to with '/' or './', the module is loaded from the node_modules folder.
If the specified path does not exist, require() will throw an error.
NOTE: Considering that on the IIS host,
.jsfiles can be accessed directly through the browser, so if you do not want to leak the source code, you can also use any file suffix.
If the module name in require() is not a local module and does not start with '/', '../', or './', then node will start from the parent directory of the current module and try to add it to / Load the corresponding module in the node_modules folder.
If it is not found, then move up to the parent directory until you reach the top directory location.
For example, if the file located at '/home/ry/projects/foo.js' calls require('bar.js'), then the locations node looks for are:
/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
You can put programs and libraries into a separate folder and provide a single entry point to it. There are three ways to enable a folder to be loaded as a parameter to require().
The first step is to create a file called package.json in the root directory of the folder, which needs to specify a main module. Below is an example of a package.json file.
{
"name" : "some-library" ,
"main" : "./lib/some-library.js"
}If this file in the example is placed under the ./some-library directory, then require('./some-library') will load ./some-library/lib/some-library.js.
If there is no package.json file in the directory, node will try to load index.js in this path.
Modules are cached after they are first loaded. This means that (like other caches) the same object will be returned every time require('foo') is called, and of course, the same file must be parsed each time.
Calling require(foo) multiple times does not necessarily cause the code in the module to be executed multiple times. This is an important feature. With this feature, it is possible to return partially completed objects; this way, transitive dependencies can also be loaded, even if they may cause loops rely.
If you want a module to execute multiple times, export a function and then call that function.
Module caching relies on parsed filenames. Since different files may be parsed depending on the location of the call (for example, when loading from the node_modules folder), there is no guarantee that require('foo') will always return the exact file if it is parsed to other files. same object.
In each module, the variable module is a reference to an object representing the current module. In particular, module.exports can be obtained through the global module object exports. module is not actually a global object, but more internal to each module.
The module.exports object is generated through the module system. Therefore, you only need to assign the object to be exported to module.exports . For example, we can also use the following method to write circle.js, which is completely equivalent.
// circle.js
var PI = Math . PI ;
var circle = { } ;
circle . area = function ( r ) {
return PI * r * r ;
} ;
circle . circumference = function ( r ) {
return 2 * PI * r ;
} ;
module . exports = circle ; Identifier used to distinguish modules. Usually the fully parsed file name.
The fully resolved file name of the module.
The module that imports this module.
When using require() to reference a module, the following process is followed to find the target module based on the expression.
require ( X ) from module at path Y
1. If X begins with './' or '/' or '../'
a . LOAD_AS_FILE ( Y + X )
b . LOAD_AS_DIRECTORY ( Y + X )
2. LOAD_NODE_MODULES ( X , dirname ( Y ) )
3. THROW "not found"
LOAD_AS_FILE ( X )
1. If X is a file , load X as JavaScript text . STOP
2. If X . js is a file , load X . js as JavaScript text . STOP
3. If X . json is a file , parse X . json to a JavaScript Object . STOP
LOAD_AS_DIRECTORY ( X )
1. If X / package . json is a file ,
a . Parse X / package . json , and look for "main" field .
b . let M = X + ( json main field )
c . LOAD_AS_FILE ( M )
2. If X / index . js is a file , load X / index . js as JavaScript text . STOP
3. If X / index . json is a file , parse X / index . json to a JavaScript object . STOP
LOAD_NODE_MODULES ( X , START )
1. let DIRS = NODE_MODULES_PATHS ( START )
2. for each DIR in DIRS :
a . LOAD_AS_FILE ( DIR / X )
b . LOAD_AS_DIRECTORY ( DIR / X )
NODE_MODULES_PATHS ( START )
1. let PARTS = path split ( START )
2. let I = count of PARTS - 1
3. let DIRS = [ ]
4. while I >= 0 ,
a . if PARTS [ I ] = "node_modules" CONTINUE
c . DIR = path join ( PARTS [ 0 . . I ] + "node_modules" )
b . DIRS = DIRS + DIR
c . let I = I - 1
5. return DIRS Built-in modules, like NodeJS, must be required before they can be used.
This module is used to write unit test cases for the program and is called through require('assert'). Ported directly from NodeJS.
Event processing module, directly transplanted from NodeJS.
The file operation module is compatible with the synchronous operation API methods of most NodeJS file operation modules.
HTTP request and processing module. Ported and modified from NodeJS, most of them are compatible.
This module contains a toolset for processing and converting file paths. Almost all methods only perform string transformation and do not call the file system to check whether the path is valid.
Ported and modified from NodeJS, almost fully compatible.
Encodes and decodes URLs, fully compatible with NodeJS.
Handles URL query strings, fully compatible with NodeJS.
This module contains utility functions for URL parsing. Use require('url') to call this module.
Fully compatible with NodeJS.
Auxiliary method module, compatible with NodeJS versions below 4.0.
Compiling the NodeAsp source code requires installing the node environment and installing uglifyJS globally.
Just execute node build on the command line. The compiled files are located in the bundle directory.
C:DiskprojectsNodeAsp>node build
-----------------------
# build nodeAsp success
+ build/NodeAsp.min.asp
@ 2016-03-01 13:46:04
-----------------------
MIT