NODE.JS

window+r快捷键弹出“命令行”输入‘cmd’系统弹出命令提示符窗口

命令

1.切换盘符 d:
2.进入文件夹 cd 文件夹的名字
3.执行某个文件 node 文件名

node的模块系统

http 搭建后台

/*--1.使用HTTP搭建服务器--*/
//1.引入http模块
const http=require('http');
//2.创建服务
var server=http.createServer(function(req,res){
   //console.log('服务器启动了');
   res.write('success');//响应的内容
   res.end();//响应结束
})
//3.指定端口号
server.listen(8080);
//  node中的模块系统
// 一。http搭建后台服务器

/*--1.使用HTTP搭建服务器--*/
//1.引入http模块
 const http=require('http');
//2.创建服务
var server=http.createServer(function(req,res){
    //req.url   请求的路径
    //res.write()//响应的内容
    //res.end() 响应结束
    
    switch(req.url){
      case'/1.html':
        res.write("111");
            break;
      case'/2.html':
        res.write("222");
            break;
        default:
         res.write("404");
    }    
    res.end();//响应结束
})
//3.指定端口号
server.listen(8080);

FS文件操作模块(读取和写文件)

读取文件
const fs=require('fs');
//读取文件
fs.readFile('aa.txt',function(err,data){//err错误 data数据
    if(err){
        console('cuo');
    }else{
        //console.log(data);
        console.log(data.toString());
    }
})
写文件
//写文件
var fs=require('fs');
//fs.writeFile('文件名','内容',function(){})
fs.writeFile('bb.txt','zxc',function(err){
    console.log(err);
})
fs模块结合http模块请求不同文件
// fs模块结合http模块请求不同文件
const http=require('http');
const fs=require('fs');
var server=http.createServer(function(req,res){
    // req.url   www
    var file_name='./www'+req.url;
    console.log(file_name);
    fs.readFile(file_name,function(err,data){
        if(err){
            res.write('404');
        }else{ 
            res.write(data);
        }
        res.end();
    })
});
server.listen(8080);

get方式

1.手动转换

//http://localhost:8080/?uname=jack&upwd=123  获取路径  把数据转换成对象格式{uname:jack,upwd:123}
const http=require('http');
var server=http.createServer(function(req,res){
    // console.log(req.url);//  /?uname=jack&upwd=123
    //切割 split('切割符')
    var GET={};
    var arr=req.url.split('?');//['/','uname=jack&upwd=123']
    var arr1=arr[1].split('&');//['uname=jack','upwd=123']
    for(var i=0;i<arr1.length;i++){
        var arr2=arr1[i].split('=');//['uname','jack']  [upwd,'123']
        GET[arr2[0]]=arr2[1];//{uname:jack,upwd:123}
        console.log(GET);
    }   
})
server.listen(8080);

2.querystring模块

//querystring 模块
const querystring=require('querystring');
var result=querystring.parse('uname=jack&upwd=123');
console.log(result);

url模块

//URL方式
//1.http 引入模块
const http=require('http');
//url
const url=require('url');
//2.创建服务
var server=http.createServer(function(req,res){
    var obj=url.parse(req.url,true);
    console.log(obj);
    console.log(obj.pathname);
    console.log(obj.query);
})
//3.端口号
server.listen(8080);

post

//get  post 
const http=require('http');
const querystring=require('querystring');
var server=http.createServer(function(req,res){
    
    var str='';
    req.on('data',function(data){//每次传输的数据
       str+=data;
    })
    req.on('end',function(){//数据传输完成
        var post=querystring.parse(str);
        console.log(post);//uname=jack&upwd=123
    })
});
server.listen(8080);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 2,505评论 1 37
  • 1 服务器创建 创建服务器利用require引入http模块:var http=require("http")利用...
    果木山阅读 340评论 0 1
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    Myselfyan阅读 4,103评论 2 58
  • 内容来自《Node.js开发指南》 核心模块是 Node.js 的心脏,它由一些精简而高效的库组成,为 Node....
    angelwgh阅读 926评论 0 1
  • 风花雪月千思念, 爱恨情愁以梦圆。 重山无阻勇直前, 沧海无边云做帆。 戎装锐剑断苍天, 血染云烟赤寒轩。 言明心...
    谷雨辰阅读 547评论 0 2