我有以下代码从我的index.html
文件中的表单获取输入,然后POSTs
将其输入到在localhost:8080上运行的节点脚本。然后,节点脚本接受输入并通过调用LUIS.ai API查询它,然后发回响应。然而,响应需要很长时间才能显示,我必须刷新页面并确认表单提交以获得结果。有没有更有效的方法来做到这一点。我是Node.js的新手。
app.js
//Modules
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var http = require('http');
//Variables and definitions
var app = express();
var query = null;
//LUIS.ai URL
var luisURL = "LUIS_API_URL";
var intent = null;
//body-parser
app.use(bodyParser.urlencoded({ extended: true }));
//Get and handle LUIS.ai data
function getLUISData(urlLUIS){
//Get LUIS.ai JSON data
request({url:urlLUIS, json:true}, function (error, response, body) {
intent = body
});
return intent;
}
//Get query from HTML form
app.post('/query', function(request, response) {
query = request.body.query;
luisURL = luisURL.concat(encodeURIComponent(query));
var data = getLUISData(luisURL);
response.send(data);
});
app.listen(8080);
的index.html
<!DOCTYPE html>
<html>
<body>
<form action="http://127.0.0.1:8080/query" method="post">
<input type="text" name="query"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>