nodejs实现的简单web服务器功能示例

<script src="https://www.jb51.net/jquery-1.12.1.js"></script> <script type="text/javascript"> $.ajax({ url:'http://192.168.0.114:3000',//服务器地址 type:'post', timeout:30, dataType:'json', data:{ "username":username, "password":password }, success:function(res){ console.log(JSON.stringify(res)); }, err:function(err){ alert(err); } });

nodejs代码:

var http = require('http'); var url = require('url'); var qs = require('querystring'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type':'text/plain;charset=UTF-8','Access-Control-Allow-origin':'*'}); if(req.method.toUpperCase() == 'GET'){ var query = url.parse(req.url, true).query;// 接受的数据 res.end(JSON.stringify(query)); }else if(req.method.toUpperCase() == 'POST'){ var postData = ''; req.on('data',function(data){ postData += data; //接受的数据 }); req.on('end',function(){ var query = qs.parse(postData); res.end(JSON.stringify(query));//返回的数据 }); } }).listen('3000',function(){ console.log('this is callback'); }); console.log('server is running');

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pxxpd.html