node.js随笔(1)

1 , node.js开启服务
    let http = require('http') ;
    
    http.createServer(function(req,res){
        if(req.url !== '/favicon.ico'){
            <!--设置访问头-->
            res.writeHead(200,{'Content-Type':'text/html ; charset=utf-8'}) ; 
            <!--注意charset=utf-8此处不能有空格-->
            res.write('hello') ; 
            
            res.end('world') ; 
            <!--有head必须要加res.end()-->
        }
    }).listen(8080) ; 
    
2 , 函数的调用
    (1):调用本页面的函数
        
        http.createServer(function(req,res){
          res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}) ; 
            <!--调用函数-->
            fn1(res) ; 
            res.end() ; 
        })
        
        <!--函数-->
        function fn1(res){
            res.write('hello') ; 
        }
    (2):调用另外页面的函数
        1):导出函数
            module.exports = {
                fn1(res){
                    res.write('我是南安') ; 
                },
                fn2(res){
                    res.write('我是南安1') ; 
                }
            }
        2):函数的调用
            let ontherFn = ('./pablic/fn1') ; 
            ontherFn['fn1'](res) ; 
                <!--ontherFn.fn1(res)-->
            ontherFn['fn2'](res) ; 
3 , 调用模块
    <!--teacher模块-->
    let User = require('./user') ; 
    function teacher(name,age,loving) {
        this.loving = loving ;
        User.call(this,name,age) ;
        this.getLoving = function (res) {
            res.write('我的姓名是:' + name + '我的年纪是:'+ age + '我的最爱是:' + loving) ;
        }
    }
    module.exports = teacher ;
    <!--调用teacher.js模块-->
    let User = require('./pablic/teacher') ; 
    user = new User('北月',24,'game') ; 
4 , 关于url模块的引用
    (1):引入
        let url = require('url') ; 
    (2):url.parse(url)
        <!--返回值是url的json对象-->
    <!--
        protocol: null,
        slashes: null,
        auth: null,
        host: null,
        port: null,
        hostname: null,
        hash: null,
        search: null,
        query: null,
        pathname: '/logon',
        path: '/logon',
        href: '/logon' 
    -->
5 , 路由的使用
    (1):配置router.js
    <!--
        module.exports = {
            logon:function (res) {
                res.write('login~~~~~') ;
            },
            zhuce:function (res) {
                res.write('注册') ;
            }
        } ;
    -->
    (2):引入路由
    let LuYou = require('./pablic/luyou.js') ;
    LuYou[pathName](response) ;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值