Tuesday, 14 March 2017

Return time for simple GET requests very long

I'm running Node.js, Handlebars, and Express for this very simple app. The page contains a single button that, when clicked, fires off an asynchronous GET request that should make a console.log message appear. When I click the Submit button, the first console.log pops up right away, but subsequent presses take a very long time (like minutes). Is this just the nature of asynchronous GET requests, or am I doing something wrong?

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.');
});

home.handlebars

<input type="submit" id="Submit">

main.handlebars

<!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>
</html>

buttons.js

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();
    });        
}



via Taylor Liss

No comments:

Post a Comment