node.js http服务
2016-04-25 09:41
369 查看
Http是互联网时代使用最广泛的协议,没有之一。Node.js内置了http模块,因此使用node.js搭建一个http服务非常简单。一、http实例照旧,先来一个http的"Hello world!",创建http.js文件,代码如下:
测试post请求则得到"Not found":
Node.js提供了一个简单的模块系统,可以让node.js的文件可以相互调用。模块是node.js应用程序的基本组成部分,文件与模块一一对应。也就是说一个文件就是一个模块,这些文件可以是JavaScript、json或者编译过的c/c++文件。模块调用有一个模块hello.js:
hello.js 中 exports 对象的成员函数了。运行效果如下:
//调用http模块 var http = require('http'); var server = http.createServer(); server.on('request', function(request, response) { // 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 发送响应数据 "Hello World !" response.end('Hello World !'); }).listen(8000); console.log('Http server is started.');运行http.js:
lee@mypc ~/works/nodejs/study4 $ node http.js Http server is started.这时可以看到程序打印完"Http server is started"并没有结束,而是一直占据进程(监听8000端口)。然后我们另起一个terminal,用curl测试http服务:
lee@mypc ~/works/nodejs/study4 $ curl "http://localhost:8000" Hello World !成功打印出"Hello world !"二、get请求创建另一个文件http_get.js。然后实现逻辑,接收到http请求后先判断request.method,如果不是GET则返回404。如果是GET请求,则用url模块获取参数,并返回接收到的参数。代码如下:
//调用http模块 var http = require('http'); //调用url模块 var url = require('url'); var server = http.createServer(); server.on('request', function(request, response) { if(request.method == 'GET') { var params = url.parse(request.url, true).query; params = JSON.stringify(params); //服务端打印参数 console.log('Get params:'+params); // 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 把请求参数返回给客户端 response.end(params+'\n'); } else{ response.writeHead(404, {'Content-Type': 'text/plain'}); response.end('Not found !\n'); } }).listen(8000); console.log('Http server is started.');运行http_get.js:
lee@mypc ~/works/nodejs/study4 $ node http_get.js Http server is started.用curl测试get得到正确结果:
lee@mypc ~/works/nodejs/study4 $ curl "http://localhost:8000?id=1&name=2" {"id":"1","name":"2"}
测试post请求则得到"Not found":
lee@mypc ~/works/nodejs/study4 $ curl -d "" "http://localhost:8000" Not found !三、post请求创建一个文件http_post.js。然后实现逻辑,接收到http请求后先判断request.method,如果不是POST则返回404。如果是POST请求,则获取http body,并返回接收到的内容。代码如下:
//调用http模块 var http = require('http'); var server = http.createServer(); server.on('request', function(request, response) { if(request.method == 'POST') { var data_post = ''; request.on('data', function(data){ data_post += data; }); request.on('end', function(){ //服务端打印参数 console.log('Get body:'+data_post); // 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 把请求参数返回给客户端 response.end(data_post+'\n'); }) } else{ response.writeHead(404, {'Content-Type': 'text/plain'}); response.end('Not found !\n'); } }).listen(8000); console.log('Http server is started.');运行http_post.js:
lee@mypc ~/works/nodejs/study4 $ node http_post.js Http server is started.用curl测试post得到正确结果:
lee@mypc ~/works/nodejs/study4 $ curl -d '{"username":"lee","id":1}' "http://localhost:8000" {"username":"lee","id":1}测试get请求则得到"Not found":
lee@mypc ~/works/nodejs/study4 $ curl "http://localhost:8000?id=1&name=2" Not found !
Node.js提供了一个简单的模块系统,可以让node.js的文件可以相互调用。模块是node.js应用程序的基本组成部分,文件与模块一一对应。也就是说一个文件就是一个模块,这些文件可以是JavaScript、json或者编译过的c/c++文件。模块调用有一个模块hello.js:
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;">exports<span class="token punctuation">.</span>sayhello <span class="token operator">=</span> <span class="token keyword" style="color: rgb(102, 217, 239);">function</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function" style="color: rgb(230, 219, 116);">log</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'Hello, '</span><span class="token operator">+</span>name <span class="token operator">+</span><span class="token string" style="color: rgb(166, 226, 46);">'.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code>又有一个主模块main.js:
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'./hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>hello<span class="token punctuation">.</span><span class="token function" style="color: rgb(230, 219, 116);">sayhello</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'James'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>在以上代码中,hello.js 通过 exports 对象把 sayhello 作为模块的访问接口,在 main.js 中通过 require('./hello') 加载这个模块,然后就可以直接访问
hello.js 中 exports 对象的成员函数了。运行效果如下:
<code class=" language-bash" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;">lee@mypc ~/works/nodejs/study5 $ node main.js Hello, James. </code>另一种写法,main2.js:
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> sayhello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'./hello'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>sayhello<span class="token punctuation">;</span><span class="token function" style="color: rgb(230, 219, 116);">sayhello</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'James'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>路径跟其它编程语言一样,有相对路径和绝对路径相对路径之当前目录:
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'./hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>或
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'./hello.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>相对路径之上级目录:
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'../study5/hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>或
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'../study5/hello.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>绝对路径:
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'/home/lee/works/nodejs/study5/hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>或
<code class=" language-javascript" style="padding: 0px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-color: transparent;"><span class="token keyword" style="color: rgb(102, 217, 239);">var</span> hello <span class="token operator">=</span> <span class="token function" style="color: rgb(230, 219, 116);">require</span><span class="token punctuation">(</span><span class="token string" style="color: rgb(166, 226, 46);">'/home/lee/works/nodejs/study5/hello.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>
相关文章推荐
- IIS8.0 HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置。
- 浅谈网络流的基本算法 [转]
- TCP/IP的网络客户端和服务器端程序
- 网络七层协议(转)
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- 使用HttpURLConnection下载文件
- 卷积神经网络(Convolutional Neural Networks,简称CNN)
- tcpcopy浅谈
- 简单的http代理服务器
- 简单的http代理服务器
- iOS开发系列--网络开发
- tomcat与http协议,servlet整合
- nginx https配置以及 api接口版本号rewrite
- AsyncHttpClient+Servlet实现文件上传下载
- 网络编程学习——信号驱动式I/O
- 网络编程学习——线程(一)
- 关于httpclient 请求https (如何绕过证书验证)
- java代码发送JSON格式的httpPOST请求
- 网络爬虫返回json处理数据
- android 封装Http请求的帮助类