I need some clarification on how to configure my Express routing using app.use
and router
. My understanding is that I can create a router and then attach it to a route using app.use()
to handle all routing related to that route. Can someone assist me in figuring out where my logic may be incorrect? Any help would be greatly appreciated.
Routing
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose'),
PlayingGame = mongoose.model('PlayingGame'),
FinishedGame = mongoose.model('FinishedGame');
var waiting_user = null;
module.exports = function(app) {
app.use('/game', router);
};
router.get('/game/waiting', function(req, res, next) {
if (waiting_user !== null) {
console.log('Lets put you two in game');
} else {
console.log('You need to wait for another player');
}
});
Client Call
var play = () => {
var username = username_input.val();
if (isUsernameValid(username)) {
$.ajax({
url: '/game/waiting',
type: 'GET',
})
.done(function() {
console.log("Success");
})
.fail(function() {
console.log("Error");
})
.always(function() {
console.log("Complete");
});
} else {
alert('Put in a valid username');
}
};