码迷,mamicode.com
首页 > Web开发 > 详细

node.js 函数的调用

时间:2019-05-18 19:14:08      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:div   creat   exp   并且   create   head   函数名   mod   本地   

普通本地函数的调用

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/‘);

 

node.js 函数的调用

标签:div   creat   exp   并且   create   head   函数名   mod   本地   

原文地址:https://www.cnblogs.com/LO-ME/p/10565677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!