上QQ阅读APP看书,第一时间看更新
Concatenating user.username
The first way is to remove world! and concatenate user.username. Then we can concatenate another string using the + (plus) operator, as shown in the following code:
console.log('Starting app.');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
fs.appendFile('greetings.txt', 'Hello' + user.username + '!');
Now if we run this, everything is going to work as expected. Over in Terminal, we can rerun our app. It prints Starting app:
Over in the greetings.txt file, you should see something like Hello Gary! printing to the screen, as shown here:
Using the fs module and the os module, we were able to grab the user's username, create a new file, and store it.