Node.js 深度体验

发布时间:2019-06-28 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Node.js 深度体验脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Node.js模块系统

模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的

node.js

//代码 require('./love') 引入了当前目录下的love.js文件
var love = require('./love');

love.confession();
love.reject();

love.js

// 通过 exports 对象把 confession 作为模块的访问接口

exports.confession = function() { // 告白
    console.log('A:我喜欢你!');
}

exports.reject = function() { // 拒绝
    console.log('B:我们只是好朋友~');
}

输出

Node.js 深度体验


node.js

var Reason = require('./love');
Reason = new Reason(); // 实例化模块
Reason.confession();  // 调用模块方法
Reason.reject();
Reason.setName('TK');
Reason.sayReason();

love.js

function Reason() { // 模块
    var name;
    this.setName = function(iName) { //模块内自定义方法
        name = iName;
    };
    this.sayReason = function() {
        console.log(name+':不要喜欢我 , 我一点都不喜欢你!');
    };

    this.confession = function() { // 告白
        console.log('A:我喜欢你!');
    }

    this.reject = function() { // 拒绝
        console.log('B:我们只是好朋友~');
    }
};
module.exports = Reason;

输出

Node.js 深度体验

Node.js 函数

在JavaScript中,一个函数可以作为另一个函数接收一个参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。

  • 自定义函数

nodejs.js

function sayLove(name) {
    console.log(name+'I Love You !');
}
    
function execute(functionName,value) {

    functionName(value);
}

execute(sayLove, "Silly Girl");

输出

Node.js 深度体验

  • 匿名函数

nodejs.js

function execute(functionName,value) {

    functionName(value);
}

execute(function(name){ console.log(name+'I Love You !'); }, "Silly Girl")

输出

Node.js 深度体验

Node.js 路由

  • url构成

                          url.parse(string).query
                                           |
           url.parse(string).pathname      |
                       |                   |
                       |                   |
                     ------ -------------------
http://localhost:8888/start?foo=bar&hello=world
                                ---       -----
                                 |          |
                                 |          |
              querystring(string)["foo"]    |
                                            |
                         querystring(string)["hello"]
  • 实例

nodejs.js

var http = require("http");
var url = require("url");
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    var query = url.parse(request.url).query;
    var name = url.parse(request.url, true).query.name;
    var age = url.parse(request.url, true).query.age;
    console.log('url:' + request.url); // 请求url
    console.log('pathname:' + pathname); // url路径
    console.log('query:' + query); // ?后请求参数
    console.log('name:' + name); // 参数name的值
    console.log('age:' + age); // 参数age值
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Get URL");
    response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server has started.");

运行

http://127.0.0.1:8888/start?name=TK&age=28

引用文字
输出

Node.js 深度体验

脚本宝典总结

以上是脚本宝典为你收集整理的Node.js 深度体验全部内容,希望文章能够帮你解决Node.js 深度体验所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: