You're calling res.setHeader, res.json and res.end *before* the userJSON variable has been filled with data. userJSON is not being filled with data until the anonymous function you give to connection.query runs, which won't be until the database has had time to process the query. You need to call your res functions in that anonymous function.
app.get('/getUsers', function (req, res) {
var users= [];
var userJSON="";
connection.query('SELECT * FROM USER ORDER BY ID', function (err, rows, fields) {
res.setHeader('Content-Type','application/json');
if (err) {
console.log('There was an error\n' + err);
res.json({error: err});
}
else {
for (var i in rows) {
users.push({ID:rows[i].ID, Username:rows[i].Username, Email:rows[i].email, Password: rows[i].Password});
}
userToJson = JSON.stringify(users);
console.log(userToJson);
res.json(userJSON);
}
res.end();
});
});