node.js+express,实现RESTful API
2015-07-09 16:25
531 查看
node代码如下(exptest.js):
patientinfo.js(位于node_entity目录下,可自定义)代码如下:
通过C#编写一个winform程序,代码如下:
WinForm程序运行界面如下:
在Windows命令窗口运行node exptest.js,然后依次点击WinForm程序上的按钮,结果如下:
var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var patientinfo=require('./node_entity/patientinfo'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.get('/api/patientinfo',patientinfo.get); app.post('/api/patientinfo',patientinfo.update); app.put('/api/patientinfo',patientinfo.put); app.delete('/api/patientinfo',patientinfo.delete); app.listen(3000);
patientinfo.js(位于node_entity目录下,可自定义)代码如下:
exports.get = function(req, res){ res.setHeader('Content-Type', 'application/json;charset=utf-8'); res.send('get patientinfo info ok'); console.log('遍历参数:'); for(var key in req.query) console.log('%s = %s',key,req.query[key]); console.log('patientinfo get ok!'); }; exports.delete = function(req, res){ res.setHeader('Content-Type', 'application/json;charset=utf-8'); res.send({status:"success", message:"delete patientinfo success"}); console.log('遍历参数:'); for(var key in req.body) console.log('%s = %s',key,req.body[key]); console.log('patientinfo delete ok!'); }; exports.update = function(req, res){ res.setHeader('Content-Type', 'application/json;charset=utf-8'); res.send({status:"success", message:"update patientinfo success"}); console.log('遍历参数:'); for(var key in req.body) console.log('%s = %s',key,req.body[key]); console.log('patientinfo update ok!'); }; exports.put = function(req, res){ res.setHeader('Content-Type', 'application/json;charset=utf-8'); console.log(req.body); res.send({status:"success", message:"add patientinfo success"}); console.log('遍历参数:'); for(var key in req.body) console.log('%s = %s',key,req.body[key]); console.log('patientinfo put OK!'); };
通过C#编写一个winform程序,代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } enum Method { POST, GET, PUT, DELETE } private string MyWebRequest(string webUrl,Method method, IDictionary<string, string> parameters, Encoding dataEncode) { string ret = string.Empty; try { string paramData = ""; if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); } i++; } paramData = buffer.ToString(); } if (method == Method.GET) { webUrl += "?" + paramData; } HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(webUrl)); webReq.Method = method.ToString(); webReq.ContentType = "application/x-www-form-urlencoded"; //webReq.ContentType = "text/html"; if (method != Method.GET) { byte[] byteArray = dataEncode.GetBytes(paramData); //转化 webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); } HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); // ret = HttpUtility.UrlDecode(ret); sr.Close(); response.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return ret; } private void button2_Click(object sender, EventArgs e) { Dictionary<string, string> param = new Dictionary<string, string>(); param["Function"] = "StudioList"; param["UserJID"] = "windows01@win-nq66026cp12"; string ret = MyWebRequest("http://127.0.0.1:3000/api/patientinfo", Method.POST, param, Encoding.UTF8); MessageBox.Show(ret); } private void button3_Click(object sender, EventArgs e) { Dictionary<string, string> param = new Dictionary<string, string>(); param["Function"] = "StudioList"; param["UserJID"] = "windows01@win-nq66026cp12"; string ret = MyWebRequest("http://127.0.0.1:3000/api/patientinfo",Method.DELETE, param, Encoding.UTF8); MessageBox.Show(ret); } private void button4_Click(object sender, EventArgs e) { Dictionary<string, string> param = new Dictionary<string, string>(); param["Function"] = "StudioList"; param["UserJID"] = "windows01@win-nq66026cp12"; string ret = MyWebRequest("http://127.0.0.1:3000/api/patientinfo",Method.PUT, param, Encoding.UTF8); MessageBox.Show(ret); } private void button5_Click(object sender, EventArgs e) { Dictionary<string, string> param = new Dictionary<string, string>(); param["Function"] = "StudioList"; param["UserJID"] = "windows01@win-nq66026cp12"; string ret = MyWebRequest("http://127.0.0.1:3000/api/patientinfo",Method.GET, param, Encoding.UTF8); MessageBox.Show(ret); } } }
WinForm程序运行界面如下:
在Windows命令窗口运行node exptest.js,然后依次点击WinForm程序上的按钮,结果如下:
相关文章推荐
- Node.js 入门
- node.js通过edge访问.net动态链接库
- LeetCode——Count Complete Tree Nodes
- ubuntu / Ubuntu Kylin下安装和卸载 Nodepadqq
- Django:ExtendsNode: extends 'base/base.html'> must be the first tag in the template.
- Populating Next Right Pointers in Each Node
- Innode表空间碎片优化
- Swap Nodes in Pairs
- node js 打包文件
- MAC下安装node.js
- 动手编写Node的C++模块
- Reverse Nodes in k-Group
- Hbase出现ERROR: Can't get master address from ZooKeeper; znode data == null解决办法
- [leedcode 25] Reverse Nodes in k-Group
- 24. Swap Nodes in Pairs
- 用C++为nodejs 写组件,提高node处理效率
- SHUTDOWN_MSG: Shutting down NameNode at java.net.UnknownHostException
- Node.js的DES加解密和MD5加密
- 【nodejs】json value出现 undefined 将会无法解析 问题来了
- nodejs npm常用命令