zoukankan      html  css  js  c++  java
  • node的function函数和路由代码的小例子

    1、node事件循环

    事件:

    const events=require("events");
    emt=new events.EventEmitter();
    
    function eventHandler(){
        console.log("111");
        console.log("222")
    }
    emt.on("eventName",eventHandler);
    emt.emit("eventName");

    2、模块自定义

    function show(){
        this.name='user1';
        this.say=function(){
            console.log("my name is "+this.name);
        }
    
    }
    module.exports=new show();

    3、function函数,分为常用函数和匿名函数

    1)、常用函数,如

             Function show(){

          Console.log(“123”);

    }

    2)、匿名函数,如

             Show=function(){

         Console.log(“123”);

    }

    Show();

    4、node路由

    代码示例如下:

    const http=require("http");
    const url=require("url");
    
    cs=function(req,res){
        ur=req.url;
        if(ur!=="/favicon.ico"){
            //arr=url.parse(ur);
            //路由
            path=url.parse(ur).pathname;
            switch(path){
                case "/user/add":
                    res.write("<h1>use add</h1>");
                    break;
                case "/user/delete":
                    res.write("<h1>use delete</h1>");
                    break;
                case "/user/select":
                    res.write("<h1>use select</h1>");
                    break;
                default:
                    res.write("<h1>hello~</h1>")    
            }
        }
        //console.log(arr);
        res.write("hello world");
        res.end();
    
    }
    
    http.createServer(cs).listen(888);
    console.log("server is ok");
  • 相关阅读:
    parallel-fastq-dump是一个大坑
    生信软件安装(2)
    2018年一些感悟
    raw data/PF data/Q30 data/clean data的不同
    专题
    结构体
    指针和数组
    指针
    函数的声明
    C语言中的函数
  • 原文地址:https://www.cnblogs.com/dreamtown/p/13328422.html
Copyright © 2011-2022 走看看