【发布时间】:2021-05-25 15:48:09
【问题描述】:
我正在尝试调整此 github 存储库中的代码:Github repo 以在 Google 应用引擎上使用,但我在将变量从 /app.js 传递到 /books/crud.js 时遇到问题。我已将 google 身份验证添加到 app.js,因此可以在该文件中访问用户的电子邮件地址,但是当我尝试导出该变量并在 crud.js 中访问它时,它不起作用。我已经尝试将变量添加到 app.js 底部的导出中,因此它显示为:
module.exports = { app, email };
然后使用以下语句将其导入 crud.js:
const appFile = require('../app');
并将变量从 crud.js 传递给 .pug 文件:
router.get('/add', (req, res) => {
res.render('books/form.pug', {
book: {},
action: 'Add',
user: appFile.email
});
});
但这不起作用,并且变量没有通过。任何帮助,将不胜感激! 这是我正在使用的完整 app.js 文件:
'use strict';
// [START getting_started_auth_all]
const express = require('express');
const metadata = require('gcp-metadata');
const {OAuth2Client} = require('google-auth-library');
const app = express();
const oAuth2Client = new OAuth2Client();
var user = "";
// Cache externally fetched information for future invocations
let aud;
// [START getting_started_auth_metadata]
async function audience() {
if (!aud && (await metadata.isAvailable())) {
let project_number = await metadata.project('numeric-project-id');
let project_id = await metadata.project('project-id');
aud = '/projects/' + project_number + '/apps/' + project_id;
}
return aud;
}
// [END getting_started_auth_metadata]
// [START getting_started_auth_audience]
async function validateAssertion(assertion) {
if (!assertion) {
return {};
}
// Check that the assertion's audience matches ours
const aud = await audience();
// Fetch the current certificates and verify the signature on the assertion
// [START getting_started_auth_certs]
const response = await oAuth2Client.getIapPublicKeys();
// [END getting_started_auth_certs]
const ticket = await oAuth2Client.verifySignedJwtWithCertsAsync(
assertion,
response.pubkeys,
aud,
['https://cloud.google.com/iap']
);
const payload = ticket.getPayload();
// Return the two relevant pieces of information
return {
email: payload.email,
sub: payload.sub,
};
}
// [END getting_started_auth_audience]
//[START getting_started_auth_front_controller]
let email = 'None';
app.get('/', async (req, res) => {
const assertion = req.header('X-Goog-IAP-JWT-Assertion');
try {
const info = await validateAssertion(assertion);
email = info.email;
} catch (error) {
console.log(error);
}
res.status(200).send(`Hello ${email}`).end();
});
// [END getting_started_auth_front_controller]
app.set('views', require('path').join(__dirname, 'views'));
app.set('view engine', 'pug');
// Books
app.use('/books', require('./books/crud'));
app.use('/api/books', require('./books/api'));
// Redirect root to /books
app.get('/', (req, res) => {
res.redirect('/books');
});
app.get('/errors', () => {
throw new Error('Test exception');
});
app.get('/logs', (req, res) => {
console.log('Hey, you triggered a custom log entry. Good job!');
res.sendStatus(200);
});
// Start the server
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
module.exports = {
app,
email
};
【问题讨论】:
-
JavaScript 在环境而非文件中执行。它没有在文件之间传递变量的概念。 (AFAIK)
标签: javascript node.js express google-app-engine google-cloud-platform