Dom framework cannot be released as scheduled because it runs to another city and has no computers. In this period, the most urgent need was to seek a super lightweight backend to build up my framework, so I reached out to the legendary Server-Side Javascrpt. Backend JS is undoubtedly the node. js of Ryan Dahl, and jaxer developed by the aptana IDE provider.
First download node. js, decompress it to the E disk, change it to node, then Enter cmd in the menu, and use the cd command to switch to the decompressed directory of nodejs:
Example 1: hello world.
Create a hello. js file in the node directory, and then enter:
- var sys = require("sys");
- sys.puts("Hello world");
Then, enter the node hello. js command in the nameboard to view the output result Hello world.
Example 2: hello world2.
Okay. This time we try to output hello world from the browser. Create http. js in the node directory and enter:
- var sys = require("sys"),
- http = require("http");
- http.createServer(function(request, response) {
- response.sendHeader(200, {"Content-Type": "text/html"});
- response.write("Hello World!");
- response.close();
- }).listen(8080);
- sys.puts("Server running at http://localhost:8080/");
Then, enter the command node http. js in the naming stage, and enter http: // localhost: 8080/in the browser/
Example 3: hello world2.
Node. js provides a Buffer class for converting strings of different encodings. Currently, three types are supported: 'ascii ', 'utf8', and 'binary '. For details, see here.
- var Buffer = require('buffer').Buffer,
- buf = new Buffer(256),
- len = buf.write('\u00bd + \u00bc = \u00be', 0);
- console.log(len + " bytes: " + buf.toString('utf8', 0, len));
Example 4: hello world3.
- // Synopsis. js
- // Synopsis abstract, synopsis, outline
- Var http = require ('http ');
- Http. createServer (function (request, response ){
- Response. writeHead (200, {'content-type': 'text/plain '});
- Response. end ('Hello World \ n ');
- }). Listen (8124 );
- Console. log ('server running at http: // 127.0.0.1: 8124 /');
Front-end address bar: http: // localhost: 8124/
Source: http://www.cnblogs.com/rubylouvre/