Nodejs模塊的調(diào)用操作實(shí)例分析
更新時(shí)間:2018年12月25日 08:42:08 作者:ChouCat
這篇文章主要介紹了Nodejs模塊的調(diào)用操作,結(jié)合實(shí)例形式分析了nodejs模塊的定義與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了Nodejs模塊的調(diào)用操作。分享給大家供大家參考,具體如下:
User.js
//構(gòu)造方法
function User(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
this.enter = function () {
console.log(this.name + "進(jìn)入國(guó)家圖書(shū)館");
}
}
/*
function User() {
this.id;
this.name;
this.age;
this.enter = function() {
console.log(this.name + "進(jìn)入圖書(shū)館");
}
}
*/
module.exports = User;
Teacher.js
var User = require('./User');
function Teacher(id, name, age) {
User.apply(this, [id, name, age]);//類的繼承
this.teach = function(res) {
res.write(this.name + "講課");
}
}
module.exports = Teacher;
modalcall_1.js
//----------------------n3_modalcall.js模塊的調(diào)用-------------
var http = require('http');
var User = require('./model/User');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
if (request.url !== "/favicon.ico") { //清除第2此訪問(wèn)
user = new User(1, "jack", 20);
//user.id = 1;
//user.name = "張三";
//user.age = 20;
user.enter();
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
modalcall_2.js
//----------------------n3_modalcall.js-------------
var http = require('http');
var Teacher = require('./model/Teacher');
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
if(request.url !== "/favicon.ico") { //清除第2此訪問(wèn)
teacher = new Teacher(1, "JackLi", 20);
teacher.enter();
teacher.teach(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
windows離線環(huán)境安裝node-sass全過(guò)程
文章介紹了如何在Windows系統(tǒng)上安裝和配置node-sass,并提供了一個(gè)詳細(xì)的步驟指南,首先,通過(guò)命令行查看支持版本;然后,下載對(duì)應(yīng)版本的node-sass安裝包;接著,在npm配置文件中增加SASS_BINARY_PATH路徑配置;最后,執(zhí)行npmi命令完成安裝2024-12-12
在node環(huán)境下parse Smarty模板的使用示例代碼
這篇文章主要介紹了在node環(huán)境下parse Smarty模板的使用示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
NestJS核心概念之Middleware中間件創(chuàng)建使用示例
這篇文章主要為大家介紹了NestJS核心概念之Middleware中間件創(chuàng)建使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
使用Node.js實(shí)現(xiàn)一個(gè)簡(jiǎn)單的命令行工具
這篇文章主要為大家詳細(xì)介紹了如何用 Node.js 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的命令行工具,模仿常用的 ls 命令,包括其 -a 和 -l 參數(shù)的功能,感興趣的可以了解下2024-11-11
node.js中的fs.readdirSync方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.readdirSync方法使用說(shuō)明,本文介紹了fs.readdirSync方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
Node.js利用debug模塊打印出調(diào)試日志的方法
debug日志打印模塊主要實(shí)現(xiàn)功能是帶命名空間(模塊名)、時(shí)間戳、色彩輸出日志;將日志寫入文件;瀏覽器端使用;格式化函數(shù);支持自定義方法。下面這篇文章主要介紹了Node.js利用debug模塊打印出調(diào)試日志的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04

