Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs ReferenceError:未定义oneTimeCode_Javascript_Node.js - Fatal编程技术网

Javascript nodejs ReferenceError:未定义oneTimeCode

Javascript nodejs ReferenceError:未定义oneTimeCode,javascript,node.js,Javascript,Node.js,我在尝试解析(主体解析器)http post响应时遇到问题。我想使用oneTimeCode作为变量,并在服务器端计算该变量。我收到以下错误:ReferenceError:oneTimeCode未定义我缺少什么 nodejs var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var fs = require('fs'); var https = requir

我在尝试解析(主体解析器)http post响应时遇到问题。我想使用
oneTimeCode
作为变量,并在服务器端计算该变量。我收到以下错误:
ReferenceError:oneTimeCode未定义
我缺少什么

nodejs

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var fs = require('fs');
var https = require('https');
var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem'),
  rejectUnauthorized: false
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

app.use(bodyParser.urlencoded({ extended: true }));
app.post('/verify', function(request, response) {
  if (oneTimeCode == '123456') {
    response.sendFile('/home/ubuntu/index.html');
  } else {
    response.sendFile('/home/ubuntu/otp.html');
  }
 });
app.get('/', function(request, response){
    response.sendFile('/home/ubuntu/otp.html');
});
https.createServer(options,app).listen(443);
otp.html

<!DOCTYPE html>
<html>
<title>OTP</title>
<body>
<h1>OTP</h1>
<form action="https://1.2.3.4.com/verify" method="post" target="_blank">
  OTP <input type="text" name="oneTimeCode"><br>
  <input type="submit" value="Submit">
</form>

<p>Click on the submit button.</p>

</body>
</html>

检察官办公室
检察官办公室
OTP
点击提交按钮


oneTimeCode不会声明为全局变量,它将在request.body中可用

你可以这样得到它:

app.post('/verify', function(request, response) {
  var oneTimeCode = request.body.oneTimeCode; // Now you have your variable
  if (oneTimeCode == '123456') {
    response.sendFile('/home/ubuntu/index.html');
  } else {
    response.sendFile('/home/ubuntu/otp.html');
  }
 });

使用
request.body.oneTimeCode
bodyParser
增强
request
变量

oneTimeCode在哪里定义?你不是从http请求中得到它吗?我的目的是通过http将
oneTimeCode
的值发回…我需要在服务器端定义该变量吗?是的,正如ezakto在回复中提到的那样