nodejs ReferenceError:oneTimeCode未定义

我在尝试parsing(body-parser)http post响应时遇到问题。 我想使用oneTimeCode作为variables,并在服务器端评估该variables。 我得到以下错误: ReferenceError: oneTimeCode is not defined我缺less什么?

的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> 

oneTimeCode不会被声明为全局variables,它将在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.oneTimeCodebodyParser增强requestvariables