自我学习之路 (一) 创建一个简单的 Http 响应服务器
发布于 7 年前 作者 YuJinLei 3472 次浏览 来自 分享

最近开始在学习 Node。 之前只用过 javascript 制作过一些简单的前端特效。 前不久了解到了 Node————表示对"TA"很有兴趣,于是就打算将自己的学习经历记录下来

	//load model http
	var http = require('http');

	/*
	 *create the http server and listen 3001 port
 	 *req is a client request
 	 *res is a server reponsive
	 */
	http.createServer(function (req, res) {

		//ressend message into the client 
		res.write('Hello,You hava started the service!');

		//on req send the data emit the event
		req.on('data', function (chunk) {

			log('Reced Data!');

		});

		//on req end emit the event
		req.on('end', function () {

			res.end();
			log('Req has closed!');

		});

	}).listen(3001);

	log('Server Start');
	log('Please open this-http://127.0.0.1:30001 in the Brower!');

	function log(str){

		console.log(str);

	}
3 回复

用nodejs的http模块创建http服务器简直易如反掌,我觉得楼主可以尝试使用 net 模块,或者使用c++的socket能对http和网络协议有着更深的理解

你可以做个socket玩玩,做个小聊天室玩。

来自酷炫的 CNodeMD

回到顶部