• 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/');
  • 相关阅读:
    出现Unexpected token, expected ","报错原因
    select属性的作用
    程序员无广告版百度
    VUE核心组件
    ajax的作用
    SSH整合
    Unity安装教程
    bean的生命周期
    BeanFactory
    打印机
  • 原文地址:https://www.cnblogs.com/LO-ME/p/10565677.html
Copyright © 2020-2023  润新知