node 创建一个服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_40257787/article/details/87009789
node创建一个服务

在创建服务之前,要先安装node以及npm包管理(现在的node都带有默认的npm包管理)

通过这个demo演示创建服务的过程

//http.js

//引入http模块
var http = require('http');
//创建一个服务
http.createServer(function(request, response){
	//request 请求体
	//response 响应体

	//writeHead 设置请求头
	response.writeHead(200, {'Content-Type': 'text/html; charset = utf-8'})
	//默认一般会有2次访问。即页面选项卡前的图标,一般建议过滤掉
	if(request.url !== '/favicon.ico'){  
		//在控制台打印
		console.log('hello');
		//网页打印
		response.write('hello world');
		//控制台打印出请求体
		console.log(request);

		//请求完成之后,要结束响应
		response.end('你好,世界');
	}

}).listen(8000);  //listen 监听本地8000端口

//提示监听的端口号
console.log('Server runningat http://127.0.0.1:8000');

猜你喜欢

转载自blog.csdn.net/sinat_40257787/article/details/87009789
今日推荐