以前使用node的包管理器npm,生成vue-cli工程模板,并且常常经过npm进行包管理,为了更好的了解nodejs包管理,学习一下nodejs
1、使用Nodejs搭建一个web服务器
默认你们已经安装好了node,直接写项目vue
1.一、建立文件夹 scott > imooc > begining
建立文件 server.jsnode
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.二、运行文件web
//cmd cd 当前目录中 scott\imooc\begining> node server
1.三、打开浏览器输入 127.0.0.1:1337
页面显示 Hello Nodejsvue-cli
2、简单的node模块
建立模块、导出模块、加载模块、使用模块npm
2.一、建立模块 school > student.js浏览器
// 编写方法 function add (stu) { console.log('New student:' + stu) } // 导出模块 exports.add = add
2.二、建立文件 school > stuInfo.js服务器
// 加载模块 var stu = require('./stu') // 使用模块 stu.add('小明')
2.三、运行文件,运行方法,显示信息学习
// cmd 当前路径,执行命令 school>node stuInfo New student:小明