Welcome 微信登录

首页 / 操作系统 / Linux / Node.js简单操作

在node中是不支持BOM和DOM操作的,所以像alert()、document.write...都是不支持的,可以是console.log()首先我们来输出“hello world”;1 "use script";2 console.log("hello world");在dos定位到你的文件位置,输入node node1  回车像下面的这些操作,用node 去执行都可以的,我就不一一执行了,有兴趣的可以自己试试var a = 5;var b = 2;console.log(a+b);//7var oDate = new Date();console.log(oDate);//当前时间下面我们来点不一样的,当然我们在dos中执行这个js文件,什么都没有是一种等待的状态,1 var http = require("http");//引入http2 //创建服务,node.js 自带的功能3 var server = http.createServer(function(require,response){4 console.log("有人来了");5 });6 server.listen(80);//端口监听这是时候们在打开浏览器 输入localhost:80  等等 你设置的端口,因为我的默认就是80端口,所以可以不写80,浏览器会一直这个状态,好像没有加载完一样,原因是http它的协议尾没有发出来dos 给我们反馈,监听到80端口有人访问,但是执行了两次console.log(),第一次正常走过,但是由于浏览器的原因,她又执行了一次,这次它获取了一个叫/favicon.ico 的东西下面我们来解决 上面两个问题 1 var http = require("http"); 2 //创建服务 3 var server = http.createServer(function(request,response){ 4 //request请求 5 //response 响应 6 if(request !== "/favicon.ico"){ //清除两次访问 7 console.log("有人来了");//会执行两次 8 response.write(request.url); //根目录 9 response.write("");//返回结果10 response.end();//结果,不结束会一直运行,end中可以放结束返回的内容11 }12 });13 server.listen(80);下面关于Node.js的内容你可能也喜欢:Ubuntu 16.04 64位 搭建 Node.js NodeJS 环境 http://www.linuxidc.com/Linux/2016-09/135487.htm在 Ubuntu 14.04/15.04 上安装配置 Node.js v4.0.0  http://www.linuxidc.com/Linux/2015-10/123951.htm如何在CentOS 7安装Node.js http://www.linuxidc.com/Linux/2015-02/113554.htmUbuntu 14.04下搭建Node.js开发环境  http://www.linuxidc.com/Linux/2014-12/110983.htmNode.Js入门[PDF+相关代码] http://www.linuxidc.com/Linux/2013-06/85462.htmNode.js开发指南 高清PDF中文版 +源码 http://www.linuxidc.com/Linux/2014-09/106494.htmNode.js入门开发指南中文版 http://www.linuxidc.com/Linux/2012-11/73363.htmUbuntu 编译安装Node.js http://www.linuxidc.com/Linux/2013-10/91321.htmNode.js 的详细介绍:请点这里
Node.js 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139431.htm