I am planning to code my node.js web page.
Im using node.js with express and jade templates.
I want to make it fully asynchronized using POST AJAX requests to server. I meant that there will be only one route like:
router.get('/', function(req, res) {
res.render('index', { "some": 'parameters'});
}
I am calling requests via client ajax like:
$("#logout").click(function(event){
event.preventDefault();
var payload = {
command: 'logout'
};
$.ajax({
url: "/main_page_ajax",
type: "POST",
datatype: "json",
contentType: "application/json; charset=utf-8",
processData: false,
data: JSON.stringify(payload),
success: function (result) {
var json = jQuery.parseJSON(result);
if(json.done==1){
$('#login_screen_clear').fadeIn("slow");
$('#login_screen_logged').hide();
show_message('Logout successful!');
}
else show_message('Something went wrong...!');
}
});
});
Part of jade template(index.jade):
#login_screen_logged
.log_div
h3 Hello
b#logged_name
| !
br
br
a#logout(href='/logout') Logout
#login_screen_clear
//login form
input#player_login(type='text', size='15', autocomplete='off')
input#player_pass(type='password', size='15', autocomplete='off')
input#login_button(type='submit', value='Login')
Back to the server side. Handling ajax requests:
router.post('/main_page_ajax', function(req, res) {
switch(String(req.body.command)){
case 'logout':
var resultJson = { };
req.session.destroy(function(err) {
resultJson.done=1;
res.end(JSON.stringify(resultJson));
});
break;
}
});
Ok so here are my questions:
Is this a good way for the node.js application?
This way will make me to store almost all containers in index.jade template (hidden divs) which will wait for specific calls. So this file will grow a lot with more app development. Is there a better place to keep html/jade code? Maybe Ajax requests itselt? Example
success: function (result) {
var json = jQuery.parseJSON(result);
if(json.done==1){
$('#login_screen_clear').html("<a><bunch><of><html><code>")
$('#login_screen_clear').fadeIn("slow");
$('#login_screen_logged').hide();
show_message('Logout Successful!');
}
else show_message('Something went wrong...!');
}
Well I hope someone will understand my concerns and point me in the correct way.
PS. Sorry for my poor english.
Don't do this. Look up on how restful services arrange their routes. For example, your route for logging out users would look like this :
router.post('/profile/:name/logout', function(req, res) {
req.session.name = null; //Flush session if you are using express
var token = req.session.token || '';
token.invalidate(token); //Function to remove token from redis or whatever you are using
res.status(200);
});
So this route will flush any sessions and/or tokens associated with the account and send back a 200 response.
As for the jade templates, I recommend you look at handlebars templating and how to serve templates from the server. In addition to being more sensible, they are also faster. I personally use it both client side and server side.
And the get request; people usually serve the html without any authentication and when the browser receives the payload, client side javascript authenticates the user via ajax and/or jwt.
Oh and your english is pretty spot on.