zoukankan      html  css  js  c++  java
  • node学习笔记_01 环境搭建

    一.下载安装nvm (node版本管理器),方便以后版本切换

      nvm list            -> 查看node版本(版本最好在8.0以上,不然在vsCode断点调试进不去,跟node版本有关系)

      nvm install 6.10.8   -> 安装node

      nvm use 6.4.2        ->使用的node版本号

    二.开始第一个hello world程序

        node filename.js 开启node服务

     ctrl + c 退出操作

    /*
        
        服务模块:
        打开服务:
    
        流程:
        1.引入模块,通过变量来接收
        2.通过http.createServer创建服务,注意后面跟上一个匿名函数
            req: request 请求
            res: response 响应
            两个接口
        3.通过listen监听端口号和访问地址
        4.通过res.writeHead设置网页状态码和文档内容类型
        5.res.end
    
    */
    var http = require("http");//变量http得到被引入模块"http"的所有接口
    
    //创建服务器,参数是一个回调函数,
    http.createServer(function(req,res){
        
        /*
            设置响应HTTP头部信息,
            第一个参数:传入网页状态码, 200表示请求正常
            第二个参数:设置文档内容类型,text/html 表示html文档类型,charset=UTF-8表示文档编码类型
            小知识:国内编码:GBK(国标k) gb2312 ...
        */
    
    
        res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"});
        
        res.end("hello world!");//如果没有res.end会存在"挂起"状态,也就是浏览器Tab选项卡的圈圈会一直转
    
        console.log("hello console~");//会在控制台打印
    
    }).listen(3000,"127.0.0.1");//端口号,要访问的名称(IP)

    三. req.url 输出请求的头部路由信息

    var http = require("http");//变量http得到被引入模块"http"的所有接口
    //创建服务器,参数是一个回调函数,
    
    http.createServer(function(req,res){
        
        res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"});
    
        console.log("服务器接收到了请求地址为:" + req.url );//会在控制台打印
        /**
         * 打印结果为:
         * 服务器接收到了请求地址为:/      ( 浏览器访问地址为 127.0.0.1:3000 时)
         * 服务器接收到了请求地址为:/favicon.ico (小图标)
         * 服务器接收到了请求地址为:/think ( 浏览器访问地址为 127.0.0.1:3000/think 时)
         * 服务器接收到了请求地址为:/favicon.ico (小图标)
         */
        res.end("hello world!");//如果没有res.end会存在"挂起"状态,也就是浏览器Tab选项卡的圈圈会一直转
    
        
    
    }).listen(3000,"127.0.0.1");//端口号,要访问的名称(IP)

    四.通过设置 req.url,访问路由

    var http = require("http");//变量http得到被引入模块"http"的所有接口
    
    //创建服务器,参数是一个回调函数,
    http.createServer(function(req,res){
        if( req.url == "/home" ){ 
    
            res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"});
            console.log("服务器接收到了请求地址为:" + req.url );//会在控制台打印
            /**
             * 打印结果为:
             * 服务器接收到了请求地址为:/      ( 浏览器访问地址为 127.0.0.1:3000 时)
             * 服务器接收到了请求地址为:/favicon.ico (小图标)
             * 服务器接收到了请求地址为:/think ( 浏览器访问地址为 127.0.0.1:3000/think 时)
             * 服务器接收到了请求地址为:/favicon.ico (小图标)
             */
            res.end("首页");//如果没有res.end会存在"挂起"状态,也就是浏览器Tab选项卡的圈圈会一直转
        
        }else if(req.url == "/nav"){
            res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"});
            console.log("服务器接收到了请求地址为:" + req.url );
            res.end("导航");
        }else if(req.url == "/detail"){
            res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"});
            console.log("服务器接收到了请求地址为:" + req.url );
            res.end("详情");
        }else{
            //页面不存在改变状态码为404
            res.writeHead(404,{"Content-type":"text/html;charset=UTF-8"});
    
            console.log("服务器接收到了请求地址为:" + req.url );//会在控制台打印
            res.end("页面不存在");//如果没有res.end会存在"挂起"状态,也就是浏览器Tab选项卡的圈圈会一直转
    
        }
    
    }).listen(3000,"127.0.0.1");//端口号,要访问的名称(IP)

    五.新建html页面,fs.readFile来读取文件显示

    hello.js

    /**
     * 特别注意:写目录时一定记得在前面加上./(如果从当前开始的话)
     * 因为我们不知道代码将来会在什么环境下运行(常见的环境:windows、linux)
     */
    
    
    var http = require("http");//变量http得到被引入模块"http"的所有接口
    var fs = require("fs");//fs文件系统模块
    //fs.readFile 异步读取一个文件的全部内容
    
    //创建服务器,参数是一个回调函数,
    http.createServer(function(req,res){
        if( req.url == "/home" ){
            /**
             *  读取文件:文件路径及名称,回调函数
             *  回调函数:  err:错误信息 , data:读取的数据/内存
             *  
             */
            fs.readFile("./home.html",function(err,data){//data读取到的文件内容
                res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"});
                console.log("服务器接收到了请求地址为:" + req.url );//会在控制台打印
                res.end(data);
            });
        }else if(req.url == "/css.css"){
            fs.readFile("./resource/css.css",function(err,data){//data读取到的文件内容
                res.writeHead(200,{"Content-type":"text/css"});
                console.log("服务器接收到了请求地址为:" + req.url );//会在控制台打印
                res.end(data);
            });
        }else{
            //页面不存在改变状态码为404
            res.writeHead(404,{"Content-type":"text/html;charset=UTF-8"});
            console.log("服务器接收到了请求地址为:" + req.url );//会在控制台打印
            res.end("页面不存在");//如果没有res.end会存在"挂起"状态,也就是浏览器Tab选项卡的圈圈会一直转
    
        }
    
    }).listen(3002,"127.0.0.1");//端口号,要访问的名称(IP)

    home.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link rel="stylesheet" href="css.css">
    </head>
    <body>
        Test页面~~
    </body>
    </html>

    dos 命令窗口 运行 node hello.js 

    在浏览器输入127.0.0.1:3000/html就可以看见页面了

     

  • 相关阅读:
    PAT顶级 1024 Currency Exchange Centers (35分)(最小生成树)
    Codeforces 1282B2 K for the Price of One (Hard Version)
    1023 Have Fun with Numbers (20)
    1005 Spell It Right (20)
    1092 To Buy or Not to Buy (20)
    1118 Birds in Forest (25)
    1130 Infix Expression (25)
    1085 Perfect Sequence (25)
    1109 Group Photo (25)
    1073 Scientific Notation (20)
  • 原文地址:https://www.cnblogs.com/mmzuo-798/p/7755116.html
Copyright © 2011-2022 走看看