Node.js 模块简单用法

栏目: Node.js · 发布时间: 6年前

内容简介:之前使用node的包管理器npm,生成vue-cli工程模板,而且经常通过npm进行包管理,为了更好的了解nodejs包管理,学习一下nodejs一、使用Nodejs搭建一个web服务器默认大家已经安装好了node,直接写项目

之前使用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 当前目录中
scott\imooc\begining> 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:小明

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Cult of the Amateur

The Cult of the Amateur

Andrew Keen / Crown Business / 2007-6-5 / USD 22.95

Amateur hour has arrived, and the audience is running the show In a hard-hitting and provocative polemic, Silicon Valley insider and pundit Andrew Keen exposes the grave consequences of today’s......一起来看看 《The Cult of the Amateur》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具