迁移到 Express 4
概述
Express 4 是 Express 3 的重大变化。这意味着如果您在其依赖项中更新 Express 版本,现有的 Express 3 应用程序将无法运行。
本文涵盖:
将 Express 3 应用程序迁移到 Express 4 的 一个例子。
2FysIatuN9JiwhuIpMUIfplePuzEJXU+CvIUwnpAYzT1szQ50W4DRbwHSr4ZaaRuihqkNvkPNobvymQOhbRrcqgHScLvGV3z9dA9cso1VpOsK2aRU8CCb3L7oWfDGRPe
Express 4 的变化
Ssszla8ZMk2Mv04AEEUcXCh1I1TcYqmEnLT9wQ0JvZzE3v0r3VeBkggxzzaqciK7
- FpSMUz+j42jbZgG1MRTY6Z+Z7If45IuGnwfZfBwX2AF1mnnwIkn9p8pqEWODlfowHqOg4+Yr3EBorQAkYAeJoWoryrCtdYxLhX/DTRsG/Q4QVO5NVq47RuOFpNmV0fwgzdZRe07KCKsWKTJwFzv2cso21iAGRMj1LF0gDE3NQLV10JFZSmCdPGfANf8lZt0LMkHi9wNjlKCSN+h2PMZjEh0SGJJgziS/E6hzS0/k+ip0NBazdQpgPQlcIfT3QNm5sPR7wHcBFnA714dqFE1oV1AmCttwnsb/oel21EzTtzuuTEH2UOJCrDmuigmIqy/cyXnLbweTRC4hLTmkmmqgPJ2luer0TM5pXmQ4j8e/FlsXBqDteT+1yvNNy3fALdCAj1CcScnuaapeV2S4ifagEjURHSUnMTMDi8OWRnUaJ9vbf4yKqecDqnwr+RfoOWwrVtsfhoD4O6MAEWlnK6BU9iHvs2hwxYdKyITggArwUoRNGJ5+FG8HPiJ3HtdMHseR0ZBidDKP1lgylb4uDQok+Q==
rDUk9HqtIGwX5tLBoOlbGa7ruVEBlqpuzpyJF2QfL44=
- yaukT2RFavYJjzaNU0jYpza2LEyIMxGBUjnxTHxkmtrdNxtB6m4MyQTkt9RdtwM5ty3WdOJNDGctdlO+SVoDVaE/87CIEDtga3bb5Pn1n5Xz5JOWxAK/YdSolcgKAScVEvIPOoB9wc4Pibh1QFPuPMCK1Y2Bo4OpbPd8+TMRCMCRvjl5Ye/RnZzJ4iMap8h3MwnZD3+Ygh2y9NgmZhaUaGhhRAuCRQi0mAci9/g57Ws=
Express 核心和中间件系统的变化
r0idhlKVmEKHHRfOlk03bTDX4zgqw/sUWZt/Ju5a5WuG2zrbh5DG9w3yPRtay5x6hZGWJoDsIcdChN5x/mq9et7Sb+1RiIU7Ykqg4iwUVNXoJOCBquHAo13A+dg+qhX/UfAHXf62TI+hEAmNkimQWZMRA1AkDrp5xpy3xeottRMtrn83T/qq5ARRzVA49DjpZL8io8zhX/i9/z8N35lOafg2V2tNy2x//IbWRMDBbOGyirHEReZMZKwN0bzT6YQHIafs0a9sTHjAM6FEvC16li+G9BcocuqTKHPP1AwUu2MQ5eqxZhyfoBAKsy31Opkz9PY+5s5WFIrRFXtpKSy+THAlanBwdYv/tb/O4Q0ZVqkzEWOOfBqm3WOVJO/qFXfQ
t0iZXvQTLpdZqwXm7qpzrK9O5j+jBwSF0xJDIA6Hl9Lq9Hm/JqzjtKnHu2wBWGWKFg9q973M60M6rpFquCEQGGZ3k2TqNVmuGsbLorK6TaJ5/ZZcyTSuuzn+ENVY9rjhlryCjA0p1ktpRl0Bmc56PHmAb+KqUprlSsNNT050elKOQuUR9/8ScAuInWzmXWb/
- 安装模块:
npm install --save <module-name>
- 在您的应用程序中,需要模块:
require('module-name')
- 根据其文档使用模块:
app.use( ... )
DbUTKCPx9Yi2QAIK93xDHF7UCfdPBFR6O9Cvs8DZkd+NWYEM1XYwR4roKIYwWYUoT+8Fe+QoMLwh1+XSsVAAc9O0Yh2YYoOE4qMFho6n3dg=
Express 3 | Express 4 |
---|---|
crAO8B6sA+1yEpcBtyR3iAF9ROKnOzp3W7hbtcoQqg8= | 1a+XXMQVsdeeelCPsUmDElXu1P/xdyYdHfv3GnvMDeIYVY8qVNn/AeVnXdwIe3YvGv4zEA80JMwrtwXm5oUNLLriyn59hY/zm06QmImbTz7cKex/ZXmfXs00+0m7G1XHHMOCFmNKQe+FmjND55TnV/YqJcGZzRB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/KuDZXnqJwIOjElFg0XGkY9ppHjb3n+5FBmK5ucSmUaw==
NC3wsSFJJsg/4HDfU7WUQORkYBd8b2sJqVGp/XMm7FyDs5TJEr/3KzdVC12Bzx1Urfs9Nj0+P8EW0CxM/kLk73qf+EZRVbWT9A2nkeN8Qz3UplzzNY/S/F73barVrmhsB9BVZyVtXfD5L1AH5pz6q9UhgsEoUiorGA1ldILhKIZcXoVbdXErkfp63sUre97/QISd7zcmnpaNa23qxQmOCi6F0f4bP67nWEtPbdKGGqU=
app.use 接受参数
PJ+Or1yMhh9aro4+n3h8MiGKjInKKbEjYiKbvGf5yovqBjgzqYNAUVK5q8Cq3mH9+5lucJHD6dlY9JUvNwHti4LIkvtmRRJhrzH0/QwxoCb7uON+PTzRMO7iztEN78t/LnSlwAR8cOV+9W1T1kUtG6ngAftc4YJhAqf5+EyWcxTIq7RRU4JtLi3awOwf46kHaNOEPHCidqVb68FClu+ORA==
app.use('/book/:id', (req, res, next) => {
console.log('ID:', req.params.id)
next()
})
路由系统
k7hOMfLSwK1YSsgBvCEyF6lXc6rYYCl/hTkFp/k0L/M3Zbtux2k5G1W8ySoqoVo8hBoY9gZWsIFI+/N7iR+zlHULGvaBT9VT8svo2jOIPmZfQ2tVv/atcWguHCXw1BvV1Lm5m+olYNbcCzDoCtIyHXDhSphi6fy3hn5XActnb40JhjxM/iE5nqHwfppSKy0N
ESk0AovbrTJA77fCwUsk7rjCSVfKlnuOSp7mgrUUAi7PH9B26ryLv/BadGHnXIEQYP0+7op3ynMGXbOS6Gm9V4gitxtKBOse2CS0040YcL4UtHU5hTZ1DfKCcxQbscgzpjwSj6P9xyadZA5Dvx+8lQ==
- BYuG+GOHXuJY18z3VnKXp0DanjuVSAWIvOCHNA06Fvrz2/gFERffD+agalhhncGQ2DgCIP9ZqGEGEAUHUXgaGKu7Vr5ZT4WL5XH2sQOyP9uFoF4hFw+jXJJ9bO3iFtR/cduvfL1os8ZGhfkv3wUOwcICf3seN0Zf7tSzc8EPZbGFidMIyseXABvWwowjf8+9zcr6hh2tWVyS+47fE9AizXNV4zWhaS90koKfG/aJh6e/4iauh2TWtE19oQP77gWeFLtguKHHDRxv5q3cAb9flh6fwHzEgNzpHWB5RYFQbsI=
app.route() 方法
Aj4SSw97jwI+Lou33n4jZZfmJz8Jp6gtKm3+JIGgHRuvxSnbLWkPXPBbAkUaeUpNhLf3ruBp46HqRmGpUDaw+Sm9++37+Bltrm5XNVtevS4sCBwhCalJAwSNJqBXuG+bHAG9bB0y6yxDSvHWxXEQrIFYwv+bjlueTpdByrD0wlFNY2Z2y6ZV3Tp55rthzGYZoqXYQjZh9C6Ja/fQhhN4gCzTc6WCwJu+LwLcq3QDi6VzeQvKq87fjpAjgJxoFEHDCasiPxCAGe6b6H56l5REuf9YrpiNSz5w021Pbo3DAv6is4NFCqU8CdeOmakavcdwwGLgwFTWxAn5D4f68f6y78RX204msuVs9mdBaj2SZepbOHBHPnS0Vz+J/pDQoA2lis3Xms8tn8U3P6T0QCUW1C9CWgFx3g+Mc+WEJpsv0uJW7cl9pmk6Gyw7hdTiV5UHrDeQwrCHza3vfZlTKoRJ9+10i6LA1HpRGXX3DMq/0pY=
lCZN7aT5rJ7Sw37/am60X75KQYGMJ6jt3CyoUiKol5ufyeeQglWVVI7mLtJoYQ7YDZrlpBa6Q++zGb7ghZv+7qdb1pVjdD2gZIUm3KR5TDzIzwx8GNZlkjICr0Nt+V3T
app.route('/book')
.get((req, res) => {
res.send('Get a random book')
})
.post((req, res) => {
res.send('Add a book')
})
.put((req, res) => {
res.send('Update the book')
})
express.Router 类
Y6IwRvyHHy4dOqR0M/Gn3LE6ohtp6OXpH40Kc0j8McVlK9A5qsUZkjZc5JJAfqoLALZbkDvqivTK6yRwEhioyjQzk/sTaQsYhCQ5tY0mMvJLy++Y408ioxyWux3IR3o1BiROC79GtQkGlVc8nwsVLx/cSbkuibkLpTSeUyNNQETQce2fnlsN9sYFwpaTOABu9erT4FeXm8To4XFcVyPoO6XyTT5nTWTh9KaUVpsxpxoFBJX3Gd7bd+7sPFn979SLPDy92YWLTWGIHlXsQoEuN8HPCFDFxL+nKJr+dXpp7ckA/ybDJz/FoVOykZmsJoK5PjzZZnMeUDmus4E2GAfofw8NK0xDclkNq45Uljz7zrk=
8oS0olWqVOfUgqwFrcIbCtCystZhj/NIQE/evOyw4YyoSn/OwUJb2tjRcPFVfOtj7Db+vVN7Rg2NRDIzNfpxtduWG8fNtZ2RnAoSs2k+EVq4lYN4yBnFrnUzf7jceO1i2XkW6b9d6xfWraE1FkAVJiU4Jw4UnQLbeceng6BuEOEKgKGhmqB4CjKe5myhqAXa
r5/CovU9ihIZoO4wK4l5yv7DGxpavVnPVB5YlAT26AyQskrm8A36xthcWb1bVta2R0XvXxNVIQI/pLIEAHB8OMcikck/Wx1HIOuCqsKNX9agqI3Rp5bfFG/6kTNQ8pA+5btHe2avLkQeyyceD+aeKA==
var express = require('express')
var router = express.Router()
// middleware specific to this router
router.use((req, res, next) => {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', (req, res) => {
res.send('Birds home page')
})
// define the about route
router.get('/about', (req, res) => {
res.send('About birds')
})
module.exports = router
kPdNpFQemtfOPhRo/A2GqEbWSNVYo6TlG3f1iVZt+PlmHGAkpI/4cKq3SxWkMbECU9XxKdHlBqmzLXZorIZwMw==
var birds = require('./birds')
// ...
app.use('/birds', birds)
IF9dTRA2e9Bl8NhPgCKVw+fcEd3UXfEIEc0kPVUxW5n0QNRq0Rv2gJnMTWP7PvKWI62G5mqXq1Wt41ZNQPDNlCsoufc42STHL85h32BhHpBWB0RYZb0kQ2Fdjs8yhAx0YYcHd93adpn5O5e/tG34IoL7GudqvxdLO+xq+05OCtpBiDdssaF0mjDG1LByAEnpv48R1BhKDqTrUTh90UB222OFmKNyXbA8Rl7x1gze1FY=
其他变化
DbUTKCPx9Yi2QAIK93xDHOYroXtvwC8g+nBuAF/clxIKzfTq/EuYupK+68Gj+oryQph/DQjGIwEjLG7tQK7vuQ==
对象 |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用迁移示例
3IAQFqBAuafrxNlBIb1SZyE6lYC45XkxshhXW8FiyDlAKXacgBzV0RpR+w+A4PHFHf3T1lvYWKewS41NJZsabGnuGNhVPHct3q501le1OxusMzaJYuQEiaf4pwT/FhE170tyI/eO2NvGMi7Nhol02/tRW0zCO1njLH/7j0LM+vGAcRuvZvq8c5ImzAvzrpWc
版本 3 应用程序
app.js
wxkW6yxqPS0C7utaOZBeyA9LSpfPC0tw7WmD7Dv6uMLF+9DXqVZWEgyPeH3+uKP3WyRokY6wCQBaxbI2g1TpE8A4NfcxGVI4cGviaCxa/2U=
var express = require('express')
var routes = require('./routes')
var user = require('./routes/user')
var http = require('http')
var path = require('path')
var app = express()
// all environments
app.set('port', process.env.PORT || 3000)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.use(express.favicon())
app.use(express.logger('dev'))
app.use(express.methodOverride())
app.use(express.session({ secret: 'your secret here' }))
app.use(express.bodyParser())
app.use(app.router)
app.use(express.static(path.join(__dirname, 'public')))
// development only
if (app.get('env') === 'development') {
app.use(express.errorHandler())
}
app.get('/', routes.index)
app.get('/users', user.list)
http.createServer(app).listen(app.get('port'), () => {
console.log('Express server listening on port ' + app.get('port'))
})
package.json
uZJ8huPbO14szjirtempOULG8zoH2acvNEcGbMvg8hnoSkxewEnK/UdhqrMHxFpaXH2GP0V7NS5RXCwXwdALmDqy3DD5PXNVp47bCtV7r6Y=
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.12.0",
"pug": "*"
}
}
迁移过程
iYzVHL1HVdKXaAhlTTdPME0Sq8/6QThJwEv868vNgwxsFUtH6x2E118ek0tg04ZCm+XUrnnEU81oaBokYUj3BR0BJrAVjJo6aa5lV9Bz4/uMRynekQwVyKx6y5d+EePdAJpQaBw5JyXhTRiwngDRRzY/EgHg7wJagcu6aanKZocBi11uQBqi2k15Io5Iap+U4qTFmZHA8oIGccBhSJ+sww==
$ npm install serve-favicon morgan method-override express-session body-parser multer errorhandler express@latest pug@latest --save
PPEXku8k+9QopLnPeBB6kdk135lxG7AEaQ5vYu/6L6kDd4efXFwqq+EmR+DYn4dX
mnYiDXrVdPm+YYCcM24UoscGyrLH7PMPjj0G7pGfkx7S1Y5K0ApqXHlTSD25x5uF0eUUpiX8GJDMiG5LMiEkFxF+u2dP1+nedFl9KY6y4icIlFJyjGmvlFOEhpSkGXJksfD8R7WuAOR5IOEgTk7SBdTYYIgpwSy/CiRPOOb3PpxnO5+J6bhleTz2Oto52nJw8KH+gt05VsPQ05g0Z/egkBqEJVO+uGrbBtoWHyobTvjFd54Iifv6UoqC9zHG3rZ2yWVuabhNRwdvvoaXbt2Uo+WzMTykmUInINeT9g+KtzwLO1PvnDerMzfkoH4M3bvaatc2jSiGaEz04u/LvTmTJUoBU+EuYzJjbBHosAqKFumIKdVaHofxlrOEFOE5gQYpNsHRmrygipFnNP0dbidVaL64ND/Ui1wuTMS3eQQVO3woWO8PwrlOjT9IWMchmxGhfNGd8Swyr0Quj/hwgpvzcSIRiTu8BPBt2oqt16z5X1I=
h2JM5RlC6bg91UiU2k/3EQV4icO+q3xWuHb3zEcFXeqJKk8tkOuond0yn6HZKrmbNuBceoyOGBdDHC3hrNMo153l7UwIqvRUPOsKtp+XsIOYlnAH2HDt7ZVh56Fjgw6bYHofZtvDh/ctUwZZN8rGS0rRBrX1kXso8vrNl4N0CZsDM+yAOPOM/zojRxud6nB/OjeFF0jBiLbQQQrq5QO+eY2eBhNQhMvija3M5HrEx/8=
ngHJh3jY5XuaDCd3+bR4r6qOJSjwgZszNi3hohEosIjX//0ZPRK2Eb5R/ZDBHi86i2Sj6H4mjGfUlXSrBKRBkURMLTomn+MPHUepAO5NHPnax2NUgXNlYx6gq3d/hffQYBMsx0igZ12XfEDa4Q2yq+Qrk03cwvo/gtM9/Ae4Y3w=
版本 4 应用程序
package.json
vb7nHAvGJNPKh0rFla69xQ67cXbdrolMQFHAaNXQ/oNAEZsXgJBwnHjEHjaBnw68RpQi5Kcera4dxE5fqw3yagRjcy0yJeKHs7uScCZGcaJ4FWv7EYBjViQvvIvCrg3R
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"body-parser": "^1.5.2",
"errorhandler": "^1.1.1",
"express": "^4.8.0",
"express-session": "^1.7.2",
"pug": "^2.0.0",
"method-override": "^2.1.2",
"morgan": "^1.2.2",
"multer": "^0.1.3",
"serve-favicon": "^2.0.1"
}
}
app.js
xJz/5kjkUuCUNBdIBChJklS/iNuwIdRBYJWaLNQ5ZF/T9kdLQJCL7CsQjroC32Jn8fbHd0JRupahpbFtKelhvsy+AkZppxtmKrxKOZ1H0MmSqYp5NiO5u3QnE/MYWu+VKtSHMvhMW8DDCRGJIKSG9b5Hjx19UxrnE+meC7mJpAWcRUHH/dPtfdGC8F2/7ddw
var http = require('http')
var express = require('express')
var routes = require('./routes')
var user = require('./routes/user')
var path = require('path')
var favicon = require('serve-favicon')
var logger = require('morgan')
var methodOverride = require('method-override')
var session = require('express-session')
var bodyParser = require('body-parser')
var multer = require('multer')
var errorHandler = require('errorhandler')
var app = express()
// all environments
app.set('port', process.env.PORT || 3000)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.use(favicon(path.join(__dirname, '/public/favicon.ico')))
app.use(logger('dev'))
app.use(methodOverride())
app.use(session({
resave: true,
saveUninitialized: true,
secret: 'uwotm8'
}))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(multer())
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', routes.index)
app.get('/users', user.list)
// error handling middleware should be loaded after the loading the routes
if (app.get('env') === 'development') {
app.use(errorHandler())
}
var server = http.createServer(app)
server.listen(app.get('port'), () => {
console.log('Express server listening on port ' + app.get('port'))
})
h+WCH+6zPx1c/mCVYMfr/uIO7jasKP466OuHDQJrdn6jBugKI5aoh/xhduwIsMJVS7AnaTLrcHFdEMrWbJe6giSKhJGdYe+h3vIAzk38CNYLRrM/AvlOjRJhGUk3HZ8+Y1CcA8tJoKxjsZvWcuvXES4c7nULcyCjiJU2ABX2KWhQ3zEald7rYtIELxWJx6VLz3Hcv7fvtBkYnxkGRMrbrg==
app.listen(app.get('port'), () => { console.log('Express server listening on port ' + app.get('port')) })
运行应用程序
U65Q7YtCYSUDkS29dMn29/mc2V/r8HiwLBRnirxe8BqB5lNZiinLmGg8RllMvRSzKbr0MGHa09GlZoI0HfLTNdNDPaX2TjHArGcjsg13KYxCzIj34tx2fPzaN8WdnQD8g9FCww5sByRbpWe2oJrZ2XTlOnFOkj3lpEXTlNl5B32uRd1Z9+RVYxHtMfAXqmia
$ node .
p4ecTl4J6Qixd8h5wjFjAH22w49TdWxyAkpTGaFZtCEaIkNUEXsNSxSA6HLXtiQBfSEf3z0tm7PA+XsThoHQKPgFceeXltotcrTYP9zbYfv3goAKnl2ZW4Pyy7Sq3l2eJnVbNcTPf6DiLCdCEBZJMfk7GVq4YX4IpbVZJmAjkDI=
升级到 Express 4 应用程序生成器
61IKyUlJxdi2mcjAahKsya1TXru5pDUU5Rdmczo8SUYhV53iqXVXaaRJb1X4urwnaicv8L1GgokN6kCa4NZZ62GzQR+fX4TnS6ilYJi6MOycaJB4KxJxDJ+EO5q23XgNmsGYc4QPeo8OA7aFxMGQrHwFKEgPJU/WbYMVrnk5CmAGcTB7ujhnoK17VTjrYuw+UKeDv1cC8pERoIVzWrOj70H0Ajye9ncW7VepiUKjYuamGB66YdeYOEBNl3q/+1qngyJyfaB1IebcKMbz16kkDw==
安装
p5ehPHiKVrHlEcJfYU2TsdA7XaOnYofN3i8vjlkNplVDLwv5b+uTPLxWZsiz8scIzvgYYv9eE8o/VOF2MSPnl/nF2UQeFD7/rBUiogQ9Q3WN6wv21JYcUVriZkdppW2e
$ npm uninstall -g express
eKAu2gA0Oh+5gOFu21HbKLtuuWmzu2jbdOjJ2OtOd7wj/5EEg4QraO5P34A0nRctEVLa73bMaDKoQ8U9hr4OtvK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idel7Y93epqLfodc565l/Agqc9hFbAUZ1w5ThVYFcI2dmLKWR0XjJgd4L7HZIF2qSwTExRutIMe+t4No1+V5N0twYB0GW+f73MfJsxRCflfJWMWkXyq8plBxGF2K5YIgM=
x+zwcT/+S744sxthVZimzj/6vtGJcqpMrT8lFHb04isca3R713sJHVgGouNeiBZIGT5BSXBE+cVe+lu52eJl6HNWWz8YPZsDUvprrl5C4iNZiBCu2Utg2uhGqqiCknDDlL2EiMgz/AhcvmERrhJHMqSm12T5xl7fZ58KzKvmFYrLCNChOHK8Sr7WLCVVWcl9mlqbug9E2BlpvDfWaxRuhEDwuh3yFqm81cH9/aDCO/SSZIoBl86ELQg4nWp3y2xwzEebyoBod/ABGS1y/OO3F6HCTrFcpEejbYdEOdsOkRsd7ZAJhdgGlGUOAAD3mSMb
Gh3up9VNS7XvJMK/hobEFthFXo/wtedfAvm50zLTDgGA9dV0q+u+EgyQPuM1YGYLGkfFgq0vKWjPutlRhJoQAlVEuSR7lddbWCbpeaagvTXfIaTJEuUsTccoOYrJs7ScOZfEW/Q1Ayl/GrCDVWEcLa8q1ALPpJsBFiHvEU0B/NEQrS6eEilbVMVoFkoftb8cMroAdE+Y5wSw7vzb1pfa0a2WUIfmR6PbjJIL5vPu/ztK4q3kyR5xGyyqEVaY/aFi
app.set('port', process.env.PORT || 3000)
var server = app.listen(app.get('port'), () => {
debug('Express server listening on port ' + server.address().port)
})
dEHrgJxCW+0gujg3OeutMipDwLdVZErBjJ+ut2BWF5dA1GuMgJD0fEkguFsH0hkmt1DqUWzST5AXO4hZjGj3TJWiqaUn/NO6EPzjSde/SjKnf6+3E3aGFODo6CJfGwZ8f1moNh0O4ES0TBF2ZlzPBQ==
var debug = require('debug')('app4')
ABq6NDLLcchgw15GqRvySb3aA/QcDJJWcJHc9HJ3mdtDMHv2GEfvMQ+8p23IOYy/MlFOdKs6qBfsYP1b2KocEiEVPRpEaRY4Is1vLXIFHVPqwkE7nX/FUTTzP4W58XF9PurOX1GAlUF5/+DaQRZUxwC1S1tZ4LTVg5bmh43IpiK+LlQjKLf1fHV3MYjKdpte
kctfshaKPXrS3nklr/Fp3G2AWi4Czayy9iG/clXhYlRLJOlPx8SDAZFoEnG6A+lcBy7gFcn0Ccrh7QhBk6iD/sWqcCVAk4snSFCqgdcASYXC0v00l97GxJwkvp1wyXOVQ4cnvK5m+vrKWLLKAlKKQMbj8ebSKOmYugunVgJxrAHKITjk7O4Rrc3z1dLhmNv/gJL/DBDxbGoOQniZ92TCJMkiVYiWr4Oaa9zOvX0SOBT0Ro+NAxKYHgwHxfDHG2lqcAaZrnzIu0KvM0tu07LtA2E6mdLjXJo7tB4XuVZAkGLjSFpEoYTb2tRLClRouIhIM0WlsqQaEqoXsh7059LgQQ==