node.js的安装与第一个hello world、node.js的初始化

一、下载node.js文件html

二、windows下点击安装  重复下一步便可node

三、编辑工具  EditPlus编辑器windows

四、新建保存目录的文件夹,并新建一个文本文档浏览器

五、打开EditPlus编辑器  打开新建的文本文档--选择“文件”-选择“另存为副本”   文件名后缀是.js  保存类型:选择“全部文件”  编码选择“utf-8”  保存服务器

 保存以后,编写console.log();编辑器

六、运行   在目录文件夹中  按住shift 右键选择“在此处打开命令窗口” -    在命令窗口中  输入node n1_hello.js函数

七、初始化工具

var http = require('http');

http.createServer(function (request, response) {

    response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'});  
    if(request.url!=="/favicon.ico"){  //清除第2此访问  
        console.log('访问');  
        response.write('hello,world');  
        response.end('hell,世界');//不写则没有http协议尾,但写了会产生两次访问  
    } }).listen(
8888); //监听8888端口 响应的数据 // 终端打印以下信息 console.log('Server running at http://127.0.0.1:8888/');

注:a、引入 required 模块   var http = require('http');ui

       b、http.createServer() 方法建立服务器,并使用 listen 方法绑定 8888 端口。 函数经过两个参数 request, response 来接收和响应数据编码

http.createServer(function (request, response) {
    
}).listen(8888);

 

       c、// 发送 HTTP 头部     // HTTP 状态值: 200 : OK     // 内容类型: text/plain   

 response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'}); 

 

      d、 // 发送给页面的响应数据 "Hello World" response.end();  若是不写的话  页面会一直再请求,但写了会产生两次访问 
      f、因为写了response.end();会产生两次访问,
为了清除第二次访问咱们添加if判断

          if(request.url!=="/favicon.ico"){  //清除第2此访问    }

     g、 response.write('hello,world');  是给页面写入响应后的数据 response响应数据

     k、/*  
           启动服务  
           cmd下执行:  
           node  n1_hello.js  
           浏览器访问:http://localhost:8888 
          */     

     

相关文章
相关标签/搜索