node 循序渐进

Posted justsmile2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node 循序渐进相关的知识,希望对你有一定的参考价值。

执行   

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);
View Code

http  服务器   模块化方式实现

建主文件 入口  index.js   -   写模块化功能并导出对应接口 server.js  -   运行 node index  -  访问 http://localhost:8888/ 

index.js

var start=require(‘./server‘);
start.start();
server.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;
View Code

 

 
 
 
 
 
 
 
 


以上是关于node 循序渐进的主要内容,如果未能解决你的问题,请参考以下文章

node 循序渐进

Koa框架教程

Koa 框架教程

Koa 框架教程

vscode代码片段建议bug

澄清 node.js + promises 片段