How to implement hello world with Node.js
This article mainly shows you how "Node.js realizes hello world", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "Node.js realizes hello world".
First download node.js, then extract it to disk E, rename it node, and then start the menu by typing cmd, and use cd command to switch to the decompression directory of nodejs:
*** Example: Hello world.
Create a hello.js file in the node directory and type:
var sys = require("sys"); sys.puts("Hello world");
Then we type the command node hello.js into the naming platform and see the naming platform output Hello world.
The second example is Hello World2.
Okay, this time we'll try outputting hello world from the browser. Create http.js in the node directory and type:
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 type node http.js in the naming console and http://localhost:8080/in the browser
The third example is Hello World2.
Node.js provides a Buffer class for converting strings with different encodings. There are currently three types supported: 'ascii','utf8'and' binary'. See details 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));
The fourth example is Hello World3.
//synopsis.js 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/'); above is all the content of "Node.js how to implement hello world", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!