nodejs渐入佳境[2]-Helloworld入门

nodejs helloworld

新建文件app.js:

1
2
//打印字符串
console.log('Start app.');

打开控制台,在当前目录下输入:

1
> node app.js

输出字符串Start app.

nodejs 复杂helloworld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//打印字符串
console.log('Start app.');

//导入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

执行结果为,创建greetings.txt文件夹,并且添加了Hello jackson