如何将变量从一个javascript文件传递到另一个javascript文件?
我正试图修改github repo:中的代码,以便在Google app engine上使用,但我在将变量从/app.js传递到/books/crud.js时遇到了一个问题。我已经将google身份验证添加到app.js中,因此可以访问该文件中用户的电子邮件地址,但当我尝试导出该变量并在crud.js中访问它时,它不起作用。我已尝试将变量添加到app.js底部的导出中,因此它的内容如下:如何将变量从一个javascript文件传递到另一个javascript文件?,javascript,node.js,express,google-app-engine,google-cloud-platform,Javascript,Node.js,Express,Google App Engine,Google Cloud Platform,我正试图修改github repo:中的代码,以便在Google app engine上使用,但我在将变量从/app.js传递到/books/crud.js时遇到了一个问题。我已经将google身份验证添加到app.js中,因此可以访问该文件中用户的电子邮件地址,但当我尝试导出该变量并在crud.js中访问它时,它不起作用。我已尝试将变量添加到app.js底部的导出中,因此它的内容如下: module.exports = { app, email }; 然后使用以下语句将其导入crud.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
};
不能将变量从一个js文件传递到另一个js文件。 但是您可以通过包含HTML/PHP文件来使用来自不同js文件的变量 让我们来看两个js文件: one.js
x=10;
y=2;
和two.js
x=10;
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>
控制台日志(x+y);
控制台中的输出为12
JavaScript在环境而不是文件中执行。它没有在文件之间传递变量的概念。(阿法克)