我们知道每个模块对应一个js文件,这篇写一个最简单的模块hello.js, 然后在另一个js文件(main.js)中require自定义的模块。 hello.js 复制代码 代码如下: function hello(name) { console.log('hello, '+ name); } exports.hello = hello; main.js 复制代码 代码如下: var h = require('./hello'); h.hello('snandy'); 约定:hello.js和main.js在同一个目录下,比如是node目录 打开命令行,进入node目录,执行命令 复制代码 代码如下: node main.js 可以看到命令行输出了:hello, snandy 注意 : main.js中require的参数不能是"hello" ,必须在前面加上"./"。
推荐阅读
JQuery一种取同级值的方式(比如你在GridView中)
复制代码 代码如下: <asp:GridView ID="gvReceipt" runat="server" Width="100%" AutoGenerateColumns="False" DataKeyNames="ID" CssClass="Grid" > <Columns> <asp:TemplateField> <ItemTemplate > <input type=>>>详细阅读
本文标题:NodeJS的模块写法入门(实例代码)
地址:http://www.17bianji.com/kaifa2/JS/23564.html
1/2 1