Friday 17 March 2017

res.render() works in one situation but not in another

In the code below, why does res.render('home'); work, but res.render('update'); does not?

This is running with Node.js, Express, and Handlebars.

app.js

var express = require('express');
var app = express();
app.use(express.static('public'));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', 3000);

//*****Routes*************

app.get('/',function(req,res,next){
    res.render('home');
});

app.get('/update', function(req,res,next){
    res.render('update');
});

app.listen(app.get('port'), function(){
    console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});

buttons.js

document.addEventListener('DOMContentLoaded', bindButtons);

function bindButtons(){
    document.getElementById('Submit').addEventListener('click', sendRequest());
}

function sendRequest() {
    var req = new XMLHttpRequest();
    req.open('GET', 'http://localhost:3000/update');
    req.send();
};

home.handlebars

<h1>Home Page</h1>
<input type="submit" id="Submit">

update.handlebars

<h1>Update Page</h1>

Clicking the button doesn't load the update page. I am not sure why.



via Taylor Liss

No comments:

Post a Comment