【8-23】node.js学习笔记

Node入门

//请求(require)Node.js自带的 http 模块,并且把它赋值给 http 变量
//变成了一个拥有所有 http 模块所提供的公共方法的对象
var http = require("http");
//调用http模块提供的函数: createServer 
//这个函数会返回一个对象,这个对象有一个叫做 listen 的方法
//这个方法有一个数值参数,指定这个HTTP服务器监听的端口号
http.createServer(
function(request, response)
 {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}
).listen(8888);

基于事件驱动的回调


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.");

 模块放在哪里

原文地址:https://www.cnblogs.com/achievec/p/4753055.html