![Learning Node.js Development](https://wfqqreader-1252317822.image.myqcloud.com/cover/262/36700262/b_36700262.jpg)
上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:
![](https://epubservercos.yuewen.com/5A2CA2/19470398308910006/epubprivate/OEBPS/Images/eeb699b5-0250-46f4-b4f4-45f16076b0c1.png?sign=1739355193-l3HRFJexWC6ljcJ61zuihaL2cdYYRZ3K-0-7410c33bb5c951af63d2bbd42dff8a28)
Over in the greetings.txt file, you should see something like Hello Gary! printing to the screen, as shown here:
![](https://epubservercos.yuewen.com/5A2CA2/19470398308910006/epubprivate/OEBPS/Images/48ca995f-4076-461a-8e60-c817922f81d8.png?sign=1739355193-AwsAFveFRDe6tXRjlh8bWQCl66bW9G6A-0-c0e7ba4bd2827e4b901474fbf3953f74)
Using the fs module and the os module, we were able to grab the user's username, create a new file, and store it.