zoukankan      html  css  js  c++  java
  • node.js 函数的调用

    普通本地函数的调用

    var http = require('http');
    
    http.createServer(function(request,response){
        response.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
        if(request.url!=="/favicon.ico"){           //清除第2此访问
    
            response.write(fun1());
            response.end('');
        }
    }).listen(8000);
    console.log('Server running at http://127.0.0.1:8000/');
    
    let fun1 = () => {
        console.log("fun1");
        return "你好,我是fun1"
    }

     

    调用另外一个js文件里的函数(只支持一个函数)

    首先创建一个js文件fun1.js 该文件只有一个函数fun1,并且将其导出

    function fun1(res) {
      console.log("我是fun1")
      res.write("您好,我是fun1")
    }
    
    module.exprts = fun1

    然后在node服务中进行调用fun1.js文件中的fun1函数

    let http = require('http')
    let otherfun = require('./models/fun1')
    
    http.createServer(function(request, response){
      response.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
      if(request.url!=="/favicon.ico"){           //清除第2此访问
    
        otherfun(response) // 当只有一个函数的时候otherfun就代表着fun1函数
    
        response.end('');
      }
    }).listen(8000);
    
    console.log('Server running at http://127.0.0.1:8000/');

    调用另外一个js文件里的函数(支持多个函数)

    首先创建一个js文件otherFun.js 该文件有两个函数fun1和函数fun2,并且将其封装成对象导出

    module.exports = {
      fun1: function (res) {
        console.log('我是fun1')
        res.write('您好,我是fun1')
      },
      fun2: function (res) {
        console.log('我是fun2')
        res.write('您好,我是fun2')
      }
    }

    然后在node服务中进行调用

    let http = require('http')
    let otherfun = require('./models/otherFun')
    
    http.createServer(function(request, response){
      response.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
      if(request.url!=="/favicon.ico"){           //清除第2此访问
    
        otherfun.fun1(response)// 多个函数的时候,需要后面写上函数名
        otherfun.fun2(response)// 多个函数的时候,需要后面写上函数名
    
        response.end('');
      }
    }).listen(8000);
    
    console.log('Server running at http://127.0.0.1:8000/');

    多个函数的另外一种调用方式,用字符串调用对应的函数,这样这里就可以用变量的方式

    let http = require('http')
    let otherfun = require('./models/otherFun')
    
    http.createServer(function(request, response){
      response.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
      if(request.url!=="/favicon.ico"){           //清除第2此访问
    
    
        otherfun['fun1'](response)// 多个函数的时候,需要后面写上函数名
        otherfun['fun2'](response)// 多个函数的时候,需要后面写上函数名
    
        response.end('');
      }
    }).listen(8000);
    
    console.log('Server running at http://127.0.0.1:8000/');
    let http = require('http')
    let otherfun = require('./models/otherFun')
    
    http.createServer(function(request, response){
      response.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
      if(request.url!=="/favicon.ico"){           //清除第2此访问
    
        fun2Name = 'fun2'
        otherfun['fun1'](response)// 多个函数的时候,需要后面写上函数名
        otherfun[fun2Name](response)// 多个函数的时候,需要后面写上函数名
    
        response.end('');
      }
    }).listen(8000);
    
    console.log('Server running at http://127.0.0.1:8000/');
  • 相关阅读:
    Centos系统python2.x升级python3.x
    VirtualBox安装Ghost XP
    VirtualBox检查更新失败解决办法
    PyDev+eclipse的编码问题
    redhat开启linux server
    使用VNC实现多用户登录linux系统
    使用webdav实现文档共享
    各类软件使用说明
    linux安装apache软件的过程
    JavaScript获取DOM元素位置和尺寸大小
  • 原文地址:https://www.cnblogs.com/LO-ME/p/10565677.html
Copyright © 2011-2022 走看看