express服务端
//用node开启服务
const express = require('express')
const app = express()
//const sum = require("./sum") # 方法单独封装成文件
var bodyParser = require("body-parser")
app.use(bodyParser())
//执行的方法
function sum (a,b){
return a+b
}
app.post('/get_data',function (req, res) {
let result = req.body
console.log('data:',result)
let a = parseInt(result.a)
let b = parseInt(result.b)
console.log('a:',a)
console.log('b:',b)
result = sum(a,b)
console.log('result:',result)
res.send(result.toString()) //返回数据
})
app.listen(3000,() => {
console.log('开启服务,端口3000')
})
express客户端
import requests
url = "http://127.0.0.1:3000/get_data"
data = {
"a": 1,
"b": 2
}
req = requests.post(url,data)
print(req.text)
express模版
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/geturl', function (req, res) {
let { url } = req.body;
let new_url = getUrl(url);
console.log('result:\n',new_url);
res.send(new_url);
});
app.post('/getsign', function (req, res) {
let { url } = req.body;
let sign = getSign(url);
console.log('result:\n',sign);
res.send(sign);
});
const server = app.listen(3000, function () {
console.log(
"node服务启动,监听地址为: http://127.0.0.1:3000"
)
});
以上就是express结合nodejs开启服务示例模版的详细内容,更多关于express结合nodejs开启服务 的资料请关注编程网其它相关文章!