1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > node.js+express 实现RESTful API

node.js+express 实现RESTful API

时间:2021-10-23 15:43:28

相关推荐

node.js+express 实现RESTful API

node代码如下(exptest.js):

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-urlencodedapp.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 ponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using ;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程序上的按钮,结果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。