require 導(dǎo)入其他文件
require可以執(zhí)行其他文件的內(nèi)容。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、
岳麓網(wǎng)站維護(hù)、網(wǎng)站推廣。
新建文件: nodes.js:
1
| console.log('start nodes.js');
|
app.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| //打印字符串 console.log('Start app.');
const nodes = require('./nodes.js') //導(dǎo)入node自帶modules,fs文件操作 const fs = require('fs'); //os系統(tǒng)操作 const os = require('os'); //獲取系統(tǒng)名字 var user = os.userInfo();
//添加字符串到回調(diào)函數(shù)中,并執(zhí)行回調(diào)函數(shù)。出錯(cuò)就會(huì)報(bào)錯(cuò),不出錯(cuò)就會(huì)打印出'The "data to append" was appended to file!' fs.appendFile('greetings.txt',`Hello ${user.username}`,(err) => { if (err) throw err; console.log('The "data to append" was appended to file!'); } );
|
打開控制臺(tái),在當(dāng)前目錄下輸入:
輸出字符串
1 2 3
| Start app. start nodes.js The "data to append" was appended to file!
|
require 導(dǎo)入屬性
nodes.js:
1 2 3
| console.log('start nodes.js');
module.exports.age = 25;
|
app.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| //打印字符串 console.log('Start app.');
const nodes = require('./nodes.js') //導(dǎo)入node自帶modules,fs文件操作 const fs = require('fs'); //os系統(tǒng)操作 const os = require('os'); //獲取系統(tǒng)名字 var user = os.userInfo();
//添加字符串到回調(diào)函數(shù)中,并執(zhí)行回調(diào)函數(shù)。出錯(cuò)就會(huì)報(bào)錯(cuò),不出錯(cuò)就會(huì)打印出'The "data to append" was appended to file!' fs.appendFile('greetings.txt',`Hello ${user.username} age ${nodes.age}`,(err) => { if (err) throw err; console.log('The "data to append" was appended to file!'); } );
|
打開控制臺(tái),在當(dāng)前目錄下輸入:
文件中存儲(chǔ):Hello jackson age 25
require 導(dǎo)入函數(shù)
nodes.js:
1 2 3 4 5 6
| console.log('start nodes.js');
module.exports.addNote = ()=>{ console.log('addNode'); return 'New Node'; };
|
app.js:
1 2 3 4 5 6
| console.log('Start app.');
const nodes = require('./nodes.js')
const res = nodes.addNote(); console.log(res);
|
打開控制臺(tái),在當(dāng)前目錄下輸入:
輸出字符串
1 2 3 4
| Start app. start nodes.js addNode New Node
|
require 導(dǎo)入帶參函數(shù)
nodes.js:
1 2 3 4 5 6 7 8 9 10 11
| console.log('start nodes.js');
module.exports.add = (a,b)=>{
return a+b; };
module.exports.addNote = ()=>{ console.log('addNode'); return 'New Node'; };
|
app.js:
1 2 3 4 5 6 7
| //打印字符串 console.log('Start app.');
const nodes = require('./nodes.js')
const res = nodes.add(1,2); console.log(res);
|
打開控制臺(tái),在當(dāng)前目錄下輸入:
輸出字符串
1 2 3
| Start app. start nodes.js 3
|
![nodejs漸入佳境[3]-require導(dǎo)入模塊](/upload/otherpic4/238909.jpg)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:nodejs漸入佳境[3]-require導(dǎo)入模塊-創(chuàng)新互聯(lián)
分享鏈接:
http://m.jiaotiyi.com/article/csejcj.html