摘要:本篇Node.js教程探讨了hello world的相关内容,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。
首先下载node.js,然后解压到E盘,改名为node,然后开始菜单输入cmd,用cd命令切换到nodejs的解压目录:
第一个例子:hello world。
在node目录下建立hello.js文件,然后在里面输入:
1. var sys = require("sys");
2. sys.puts("Hello world");
然后我们在命名台中输入命令node hello.js,就能看到命名台输出结果Hello world。
第二个例子:hello world2。
好了,这次我们试从游览器中输出hello world。在node目录下建立http.js,然后输入:
1. var sys = require("sys"),
2. http = require("http");
3. http.createServer(function(request, response) {
4. response.sendHeader(200, {"Content-Type": "text/html"});
5. response.write("Hello World!");
6. response.close();
7. }).listen(8080);
8. sys.puts("Server running at //localhost:8080/");
然后我们在命名台中输入命令node http.js,在浏览器输入//localhost:8080/
第三个例子:hello world2。
node.js提供一个Buffer类用于转换不同编码的字符串。目前支持三种类型:'ascii','utf8'与'binary'。详见这里
1. var Buffer = require('buffer').Buffer,
2. buf = new Buffer(256),
3. len = buf.write('\u00bd + \u00bc = \u00be', 0);
4. console.log(len + " bytes: " + buf.toString('utf8', 0, len));
第四个例子:hello world3。
1. //synopsis.js
2. //synopsis 摘要, 梗概,大纲
3. var http = require('http');
4.
5. http.createServer(function (request, response) {
6. response.writeHead(200, {'Content-Type': 'text/plain'});
7. response.end('Hello World\n');
8. }).listen(8124);
9.
10. console.log('Server running at //127.0.0.1:8124/');
本文由职坐标整理发布,学习更多的Node.js相关知识,请关注职坐标WEB前端Node.js频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号