1. node命令
node命令是用来运行Node.js脚本的命令
node helloworld.js
可以使用tab键自动补全
2. 使用 exports 从 Node.js 文件中公开功能
node.js具有内置的模块系统
也可以导入其他node.js文件公开的功能
想导入某些东西时,可以使用
const library = require('./library')
在 library.js 中,必须先公开功能,才能被导入其他文件中
这是module系统提供的module.exports API 可以做的事
可以通过两种方式进行操作
- 将对象复制给
module.exports这会使文件只导出该对象
const car = {
brand:'Ford',
model:'Fiesta'
}
module.exports = car
//在另一个文件中
const car = require('./car')
- 将要导出的对象添加
exports的属性,这种方式可以导出多个对象函数或数据
const car = {
brand:'Ford',
model:'Fiesta'
}
exports.car = car
或者直接
exports.car = {
brand:'Ford',
model:'Fiesta'
}
//在另一个文件中
const item = require('./items')
items.car
//或
const car = require('./items').car
module.exports 和 export 之间有什么区别?
前者公开了它指向的对象。 后者公开了它指向的对象的属性。
12万+

被折叠的 条评论
为什么被折叠?



