- node.js传统的hello world
如下所示
var http=require("http"); function onRequest(request, response) { console.log("Request received"); response.writeHead(200,{"Content-Type":"text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started");
- 我写的hello world
如下所示,文件名为hi.js
exports.hi = function() { return "Hello World"; }
然后,在命令行中输入如下
- 一点点注释
require函数搜索模块并加载模块到Node运行函数,然后激活此模块内的函数。如hi.js。
一般来讲,模块名就是路径名,但是不带文件的扩展名。就是说,我们写require('./hi')时,
Node知道添加为文件添加后缀名.js,并加载hi.js.
exports的作用:任何函数或对象可赋给exports,便可导出模块内部的函数或对象。如果没
有赋给exports,则模块内的任何代码在模块外都是不可见的。