基于node.js及express实现中间件,实现post、get
生活随笔
收集整理的這篇文章主要介紹了
基于node.js及express实现中间件,实现post、get
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先,當(dāng)然是有必要的環(huán)境,安裝node,這個我就不多說了。
依賴模塊:
"express": "^4.13.4", "request": "^2.72.0", "body-parser": "^1.13.3",頁面 $.ajax({type: "post",url: "/api",contentType: "application/json", //data: {"UserName":"struggle","password":123456}, //普通數(shù)據(jù)data: JSON.stringify({"formdata":{"UserName":"struggle","password":123456}}), //這里為了應(yīng)付需要傳復(fù)雜的json數(shù)據(jù)success:function(msg){console.log(msg);}});
服務(wù)端: var express = require('express'); var app = express(); var bodyParser = require('body-parser');// 解析json,need it... app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json()); app.post('/api', function(req, res,next){console.log('post-test');console.log(req.body.formdata); var request = require('request');request.post('http://192.168.1.220:8094/User/Login',{form:req.body.formdata},function(err, response, body){res.send(body); //返回的數(shù)據(jù) }); });app.use(express.static(path.join(__dirname, '/')))app.listen(8080); console.log('Listening on port 8080');
?
轉(zhuǎn)載于:https://www.cnblogs.com/wteng/p/5583972.html
總結(jié)
以上是生活随笔為你收集整理的基于node.js及express实现中间件,实现post、get的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #10
- 下一篇: 字符串分割与存入List集合