const nodes = require('./nodes.js') //导入node自带modules,fs文件操作 const fs = require('fs'); //os系统操作 const os = require('os'); //获取系统名字 var user = os.userInfo();
//添加字符串到回调函数中,并执行回调函数。出错就会报错,不出错就会打印出'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!'); } );
打开控制台,在当前目录下输入:
1
> node app.js
输出字符串
1 2 3
Start app. start nodes.js The "data to append" was appended to file!
require 导入属性
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') //导入node自带modules,fs文件操作 const fs = require('fs'); //os系统操作 const os = require('os'); //获取系统名字 var user = os.userInfo();
//添加字符串到回调函数中,并执行回调函数。出错就会报错,不出错就会打印出'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!'); } );