我如何将一个变量从一个javascript文件传递到另一个?



我试图适应这个github repo代码:github repo用于谷歌应用程序引擎,但我有一个问题,从/app.js到/books/crude .js传递一个变量。我已经添加了谷歌身份验证到app.js,所以可以访问用户的电子邮件地址在该文件,但当我试图导出该变量,并在crude .js访问它,它不工作。我尝试将变量添加到app.js底部的导出中,以便它读取:

module.exports = { app, email };

,然后用下面的语句在crude .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
};

你不能将一个变量从一个js文件传递到另一个js文件。但是你可以通过包含一个HTML/PHP文件来使用来自不同js文件的变量。

假设你有两个js文件:one.js

x=10;

two.js

y=2;
现在你有了一个index.html文件
<script type="text/javascript" src="1.js"></script>
<script type="text/javascript" src="2.js"></script>
<script>
console.log(x+y);
</script>

控制台输出为12

相关内容

  • 没有找到相关文章

最新更新