Home >Web Front-end >JS Tutorial >Server-side JavaScript script Node.js Getting started_javascript skills

Server-side JavaScript script Node.js Getting started_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:55:411102browse

First download node.js, then unzip it to the E drive, rename it to node, then enter cmd in the start menu, and use the cd command to switch to the nodejs decompression directory:

Server-side JavaScript script Node.js Getting started_javascript skills

First example: hello world.

Create the hello.js file in the node directory, and then enter:
Copy the code The code is as follows:

var sys = require("sys");
sys.puts("Hello world");

Then we enter it in the naming table Command node hello.js, and you can see the naming console output Hello world.

Second example: hello world2.

Okay, this time we try to output hello world from the browser. Create http.js in the node directory, and then enter:
Copy the code The code is as follows:

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 we enter the command node http.js in the naming platform and http://localhost:8080/ in the browser
Server-side JavaScript script Node.js Getting started_javascript skills
Server-side JavaScript script Node.js Getting started_javascript skills
The third example: hello world2.
node.js provides a Buffer class for converting strings of different encodings. Currently three types are supported: 'ascii', 'utf8' and 'binary'. See here
Copy the code The code is as follows:

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));

Fourth example: hello world3.
Copy code The code is as follows:

//synopsis.js
/ /synopsis summary, synopsis, outline
var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content- Type': 'text/plain'});
response.end('Hello Worldn');
}).listen(8124);

console.log('Server running at http ://127.0.0.1:8124/');

Front-end address bar: http://localhost:8124/

Fifth example: Compiling C files
Copy code The code is as follows:

#include #include int main(){ printf("Hello World!!!"); exit(0); }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn