南溟丷

我想在那里最蓝的大海扬帆

0%

关于Nodejs的一些开发记录

不想写课设。

Express

1
2
npx express-generator
npm install

MongoDB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# MongoDB
npm install --save mongodb mongoose
# 创建容器
docker run -p 27017:27017 -v ~/Docker/mongodb/data/:/data/db --name mongodb -d mongo
# 进入数据库
docker exec -it mongodb mongo
# 选择数据库
use admin
# 创建用户
db.createUser({user: 'south', pwd: 'south!!!!!', roles: ['root']})
# 校验
db.auth('south','south!!!!!')
# 销毁容器
docker stop mongodb && docker rm mongodb
# 创建容器
docker run -p 27017:27017 -v ~/Docker/mongodb/data/:/data/db --name mongodb -d mongo --auth
# 进入数据库
docker exec -it mongodb mongo admin
# 校验
db.auth('south','south!!!!!')
# 查看数据库
show dbs
# 创建数据库
use takeout_system
# 数据库授权
db.createUser({user: 'sea', pwd: 'sea!!!!!', roles: ['dbOwner']})
# 插入数据
db.user.insert({"south":"south"})

Alipay

1
2
# Alipay
npm install --save alipay-sdk

Api如下,支付宝沙箱环境。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var express = require('express');
var fs = require('fs');
var formidable = require('formidable');
var AlipaySdk = require('alipay-sdk').default;
var AlipayFormData = require('alipay-sdk/lib/form').default
var router = express.Router();

const alipaySdk = new AlipaySdk({
appId: '2016102300743038',
privateKey: fs.readFileSync('./config/private-key.pem', 'ascii'),
gateway: 'https://openapi.alipaydev.com/gateway.do',
signType: 'RSA2',
});

async function pay(good) {
const formData = new AlipayFormData();
formData.setMethod('get');
formData.addField('notifyUrl', 'http://127.0.0.1:3000/paycallback');
formData.addField('returnUrl', 'http://127.0.0.1:3000/success');
formData.addField('bizContent', {
outTradeNo: new Date().valueOf(),
productCode: good.orderID,
totalAmount: good.totalPrice,
subject: good.sellerName,
body: good.orderFoodList,
});
const result = await alipaySdk.exec('alipay.trade.page.pay', {}, {
formData: formData,
validateSign: true
},
);
return result;
}

router.get('/', function (req, res, next) {
res.send(`
<form action="./pay" enctype="multipart/form-data" method="post">
<input type="text" name="orderID" />
<input type="text" name="totalPrice" />
<input type="text" name="sellerName" />
<input type="text" name="orderFoodList" />
<input type="submit" value="submit" />
</form>
`);
});

router.post('/', function (req, res, next) {
const form = formidable({multiples: true});
form.parse(req, function (err, fields, files) {
if (err) {
res.send({
status: 0,
type: 'FORM_DATA_ERROR',
message: '表单信息错误'
})
return;
}
var {orderID, totalPrice, sellerName, orderFoodList} = fields;
var good = {orderID, totalPrice, sellerName, orderFoodList};
pay(good).then(result => {
res.send(result);
})
return;
});
});


router.post('/paycallback', async (ctx, next) => {
let postData = ctx.request.body;
console.log("触发付款");
if (postData.trade_status === "TRADE_SUCCESS") {
let data = ctx.request.body // 订单信息
console.log("支付完成!");
}
})

router.get('/success', async (ctx, next) => {
ctx.body = "支付成功"
})

module.exports = router;