脚本宝典收集整理的这篇文章主要介绍了Node.js 模块简单用法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
之前使用node的包管理器npm,生成vue-cli工程模板,而且经常通过npm进行包管理,为了更好的了解nodejs包管理,学习一下nodejs
一、使用Nodejs搭建一个web服务器
默认大家已经安装好了node,直接写项目
1.1、创建文件夹 scott > imooc > begining
创建文件 server.js
VAR http = require('http')
var server = http.createServer(function (req, res) {
res.wrITeHead(200,{'Content-tyPE':'text/plain'})
res.end('Hello Nodejs n')
})
server.listen(1337, '127.0.0.1')
1.2、运行文件
//cmd cd 当前目录中
scottimoocbegining> node server
1.3、打开浏览器输入 127.0.0.1:1337
页面显示 Hello Nodejs
二、简单的node模块
创建模块、导出模块、加载模块、使用模块
2.1、创建模块 school > student.js
// 编写方法
function add (stu) {
console.LOG('New student:' + stu)
}
// 导出模块
exports.add = add
2.2、创建文件 school > stuInfo.js
// 加载模块
var stu = require('./stu')
// 使用模块
stu.add('小明')
2.3、运行文件,运行方法,显示信息
// cmd 当前路径,执行命令
school>node stuInfo
New student:小明
以上是脚本宝典为你收集整理的Node.js 模块简单用法全部内容,希望文章能够帮你解决Node.js 模块简单用法所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。