执行
node helloworld.js
http 服务器
建 server.js 文件 - node server.js 跑起来 - 浏览器访问 http://localhost:8888/ (服务器控制台观看访问次数)
var http = require("http"); var count=(function(){ var num=0; return function(){ num++; return num; } }()); http.createServer(function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); response.end(); console.log(‘server‘+count()); }).listen(8888);
http 服务器 模块化方式实现
建主文件 入口 index.js - 写模块化功能并导出对应接口 server.js - 运行 node index - 访问 http://localhost:8888/
index.js
function handle(request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); response.end(); console.log(‘server‘ + count()); } var count = (function () { var num = 0; return function () { num++; return num; } }()); var http = require("http"); function start(){ http.createServer(handle).listen(8888); console.log("Server working."); } exports.start = start;
原文:https://www.cnblogs.com/justSmile2/p/9896848.html