node 模块化(简单)

demo.js

var http = require("http");
/*	require同级目录的时候需要需要加上./
 * 否则会找不到文件
 */
var User = require("./User");

var myApp = http.createServer(function(request,response){
		/*防止两次访问服务器*/
	if(request.url != "/favicon.ico"){
		console.log("开始访问服务器");
		response.writeHead(200,{"Content-Type" : "text/html;charset = utf-8"});
	
		response.write("<div style='color:red'>测试002</div>");
		
		var n1 = new User(1,"娜美","2000");
		
		response.write(JSON.stringify(n1));
	}
	
	
	response.end("关闭服务器");
});

myApp.listen(8000);

console.info("服务器启动成功");

User.js

function User(id,name,pic){
	this.id = id;
	this.name = name;
	this.pic = pic;
	this.say = function(){
		console.log(this.id+this.name+this.pic);
	}
}
/*	单个对象导出公共化 User	*/
module.exports = User;


猜你喜欢

转载自blog.csdn.net/guohao326/article/details/78268107