NODE.JS

作者: 笨小妞_9ed3 | 来源:发表于2018-12-05 14:43 被阅读0次

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);

相关文章

  • nodejs安装

    Node.js安装 目录 Node.js简单介绍 windows安装Node.js Linux安装Node.js ...

  • node.js基础

    什么是node.js Node.js特点 node.js优点和缺点

  • Nodejs.2

    参考内容:Node.js EventEmitter 四、Node.js EventEmitter Node.js所...

  • nodejs第一步

    Node.js 是什么?Node.js与JavaScript的区别是什么? Node.js的优点?Node.js的...

  • node 学习笔记.md

    Node.js第一天 1. 初识Node.js 1.1 Node.js是什么 Node.js® is a Java...

  • Node.js学习

    主线:Node.js是什么 --> Node.js的组成 --> Node.js的特点 --> Helloworl...

  • 使用AngularJS搭建前台框架

    Node.js部署: 下载安装包:从Node.js官网下载Node.js安装包。 安装Node.js:打开node...

  • Node.js模块

    Node.js 模块和 Node.js 包介绍。 一、Node.js模块 每一个Node.js都是一个Node.j...

  • 小鹅通视频下载mac 小鹅通课程下载教程

    前两天,Node.js官方发布了Node.js 15的正式版本,Node.js 15 将替代 Node.js 14...

  • node学习笔记

    node.js 介绍 node.js初识 node.js 平台是基于 Chrome V8 JavaScript 引...

网友评论

      本文标题:NODE.JS

      本文链接:https://www.haomeiwen.com/subject/jzdicqtx.html