javascript - 不知道为什么我的 app.get 运行了两次?
问题描述
我有一个app.get
which 里面有相当多的逻辑。除了某些由于某种原因被调用两次的逻辑之外,一切都很好。我注意到当我将某些内容保存到 db 时,它会保存两行。
所以我console.log
在那个区域放了一个,果然它记录了两次。
为什么会发生这种情况?
app.get('/shopify/callback', (req, res) => {
const { shop, hmac, code, state } = req.query;
const stateCookie = cookie.parse(req.headers.cookie).state;
if (state !== stateCookie) {
return res.status(403).send('Request origin cannot be verified');
}
if (shop && hmac && code) {
// DONE: Validate request is from Shopify
const map = Object.assign({}, req.query);
delete map['signature'];
delete map['hmac'];
const message = querystring.stringify(map);
const providedHmac = Buffer.from(hmac, 'utf-8');
const generatedHash = Buffer.from(
crypto
.createHmac('sha256', config.oauth.client_secret)
.update(message)
.digest('hex'),
'utf-8'
);
let hashEquals = false;
try {
hashEquals = crypto.timingSafeEqual(generatedHash, providedHmac)
} catch (e) {
hashEquals = false;
};
if (!hashEquals) {
return res.status(400).send('HMAC validation failed');
}
// DONE: Exchange temporary code for a permanent access token
const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
const accessTokenPayload = {
client_id: config.oauth.api_key,
client_secret: config.oauth.client_secret,
code,
};
request.post(accessTokenRequestUrl, { json: accessTokenPayload })
.then((accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
// DONE: Use access token to make API call to 'shop' endpoint
const shopRequestUrl = 'https://' + shop + '/admin/shop.json';
const shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
}
request.get(shopRequestUrl, { headers: shopRequestHeaders })
.then((shopResponse) => {
const response = JSON.parse(shopResponse);
const shopData = response.shop;
console.log('BEING CALLED TWICE...')
res.render('pages/brand_signup',{
shop: shopData.name
})
})
.catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});
})
.catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});
} else {
res.status(400).send('Required parameters missing');
}
});
解决方案
推荐阅读
- kubernetes - 创建新命名空间时,serviceaccout 和 secret 不会自动创建
- java - 尝试在coldfusion中编写一个java函数
- delphi - 如何在 Delphi 10.3.3 Pro 中使用 Indy 获取域名的到期日期?
- php - 在php中读取文本文件,变量和数据记录为多行
- phpstorm - 从 PhpStorm 自动完成中隐藏某些类/子目录
- python - 如何在python中将大小设置为texture_size
- laravel-5 - 使用 Laravel eloquent 获取匹配月份和年份的记录
- amazon-web-services - 为什么“除非您使用直写,否则 TTL(生存时间)通常不是一个坏主意?”
- gitlab-ci - 如何重用来自另一个 repo 的 CI/CD 管道,但添加一个额外的工作?
- c++ - 为 C 程序员阐明 C++ 中的嵌套类回调函数