Javascript 简单的GET请求需要很多时间才能返回
我正在为这个非常简单的应用程序运行Node.js、handlebar和Express。该页面包含一个按钮,单击该按钮时,会触发一个异步GET请求,该请求应会显示一条Javascript 简单的GET请求需要很多时间才能返回,javascript,html,node.js,express,asynchronous,Javascript,Html,Node.js,Express,Asynchronous,我正在为这个非常简单的应用程序运行Node.js、handlebar和Express。该页面包含一个按钮,单击该按钮时,会触发一个异步GET请求,该请求应会显示一条控制台.log消息。当我单击Submit按钮时,第一个console.log会立即弹出,但随后的按下会花费很长时间(如分钟)。这就是异步GET请求的本质,还是我做错了什么 app.js var express = require('express'); var app = express(); app.use(express.stat
控制台.log
消息。当我单击Submit按钮时,第一个console.log
会立即弹出,但随后的按下会花费很长时间(如分钟)。这就是异步GET请求的本质,还是我做错了什么
app.js
var express = require('express');
var app = express();
app.use(express.static('public'));
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 8080);
app.get('/',function(req,res,next){
var context = {};
res.render('home', context);
});
app.get('/notify',function(reg,res,next){
console.log('I got a GET request!');
});
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});
<input type="submit" id="Submit">
<!doctype html>
<html>
<head>
<title>Test Page</title>
<link rel="stylesheet" href="css/style.css">
<script src="scripts/buttons.js" type="text/javascript"></script>
</head>
<body>
{{{body}}}
</body>
</html>
document.addEventListener('DOMContentLoaded', bindButtons);
function bindButtons(){
document.getElementById('Submit').addEventListener('click', function(event){
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:8080/notify", true);
req.send(null);
event.preventDefault();
});
}
主页。把手
var express = require('express');
var app = express();
app.use(express.static('public'));
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 8080);
app.get('/',function(req,res,next){
var context = {};
res.render('home', context);
});
app.get('/notify',function(reg,res,next){
console.log('I got a GET request!');
});
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});
<input type="submit" id="Submit">
<!doctype html>
<html>
<head>
<title>Test Page</title>
<link rel="stylesheet" href="css/style.css">
<script src="scripts/buttons.js" type="text/javascript"></script>
</head>
<body>
{{{body}}}
</body>
</html>
document.addEventListener('DOMContentLoaded', bindButtons);
function bindButtons(){
document.getElementById('Submit').addEventListener('click', function(event){
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:8080/notify", true);
req.send(null);
event.preventDefault();
});
}
如果你转到,你会看到页面一直在加载,而且从来没有真正加载过
这是因为对您的请求没有响应。在您的应用程序中,后续请求花费的时间太长,因为有以前的请求尚未响应
尝试将此添加到console.log之后的/notify GET处理程序:
res.send('This is a cool message from server');
如果你转到,你会看到页面一直在加载,而且从来没有真正加载过
这是因为对您的请求没有响应。在您的应用程序中,后续请求花费的时间太长,因为有以前的请求尚未响应
尝试将此添加到console.log之后的/notify GET处理程序:
res.send('This is a cool message from server');
完美的非常感谢你!完美的非常感谢你!