如何使用Node Express JS获取完整的URL并发送到其他JS页面

时间:2018-12-03 08:21:36

标签: javascript node.js express

我正在尝试访问另一个页面(api.js)中的 token 变量,该如何使用? index.js

  var express = require('express');
        var router = express.Router();                
        router.get('/', function(req, res, next) {
        var token = req.url;
        });       
    module.exports = router;
    module.exports.token1 = token; 

api.js [我想在api.js中使用令牌变量,例如sessionId:requested.token1]

var apiai = require('apiai');
var requestd  = require('./index');
var app = apiai('-my-api-key');
var getRes = function(query) {
  var request = app.textRequest(query, {
      sessionId: requestd.token1
  });
const responseFromAPI = new Promise(
        function (resolve, reject) {
request.on('error', function(error) {
    reject(error);
});
request.on('response', function(response) {

resolve(response);
});
});
request.end();
return responseFromAPI;
};
module.exports = {getRes}

2 个答案:

答案 0 :(得分:1)

尝试

index.js

var express = require('express');
    var router = express.Router();                
    router.get('/', function(req, res, next) {
        require('./api').get(req ,res) // Be sure your path is correct
    });       
module.exports = router;

api.js

var api = {
    get: function(req, res){
        var token = req.url;
        console.log(toaken);
    }
}
module.exports = api

答案 1 :(得分:0)

请更新需要文件语句var requestd = require('./index.js'); 并在此语句module.exports.token1 = token;

之前定义“令牌”变量