NODEJS复习

-----基本模块
const http=require('http')
http.createServer((req,res)=>{
    res.setHeader('Content-Type','text/html;charset=utf-8')
    res.write('我只想要一个冰墩墩')
    res.end('end')
}).listen(8080,'127.0.0.1',()=>{
    console.log('服务器已启动,打开在网址:http://127.0.0.1:8080')
})
(自定义模块与类的定义)
index文件
-----------------------------------
const obj=require('./class')

let obiing=new obj('joe')
obiing.stu()
obiing.eat()

---------------------------------------------------
class文件
class class1{
    constructor(name){
      this.name=name
    }
    stu(){
        console.log(this.name+'学习ing')
    }
    eat(){
        console.log(this.name+'冲ing')
    }
}
module.exports=class1
模板解析
let http= require('http')
let ejs=require('ejs')
http.createServer((req,res)=>{
    let dataObj={
        name:'刘备',
        age:15,
        arr:[
            '1',
            '2',
            '3'
        ]
    }

    ejs.renderFile('index.html',dataObj,(err,data)=>{
        if(err){
            res.statusCode=404;
            res.end('找不到页面')
        }else{
            res.end(data.toString())
        }
    })


   
}).listen(2157,'127.0.0.1',()=>{
    console.log('浏览器打开')
})
循环
let arr = [
    ['中国','朝鲜','韩国','小人国'],
    ['美国','加拿大','墨西哥'],
    ['英国','德国','法国'],    
    ['俄罗斯']
];

//方式1
for (let i = 0; i < arr.length; i++) {
   for(let j=0; j<arr[i].length;j++){
       console.log(arr[i][j])
   }
    
}


// 方式2
arr.forEach((value,idex) => {
    value.forEach((value) => {
        console.log(value)
    });
});

// 方式3
for (const key in arr) {
    for (const key1 in arr[key]) {
       console.log(arr[key][key1])
    }
}

// 方式4
for (const value of arr) {
    for (const value1 of value) {
        console.log(value1)
    }
}
fs的使用示范
const fs =require('fs');
fs.readFile('./abc.txt',(err,data)=>{
    if(err){
        console.log(err)
    }else{
        console.log(data.toString())
    }
})
let http = require('http')
http.createServer((req,res)=>{
    if(req.url=='/favicon.ico'){
        return
    }
    res.setHeader('Content-Type','text/html;charset=utf-8')
    let urlObj=new URL(req.url,'http://127.0.0.1:2157')
    console.log(urlObj)
    if(urlObj.pathname=='/index/index' || urlObj.pathname=='/' ){
        res.end ('首页')
    }else if(req.method.toLocaleLowerCase()=='get'){
            res.end('登陆页面')
            console.log('本地主机地址: ',req.socket.localAddress)
            console.log('客户端IP地址:',req.socket.remoteAddress)
          
    }else{
        let allData='';
        req.on('data',(chunk)=>{
            allData+=chunk;
        })
        req.on('end',()=>{
            console.log('数据接收完毕:'+allData)
            res.end('登录')
            let temp=new URLSearchParams(allData)
            console.log(temp.searchParams.get('username'))
            
        })
    }
        
    
  



}).listen(2157,'127.0.0.1',()=>{
    console.log('浏览器打开')
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值