I'm trying to understand how jade template engine works. I would like to open one of my .jade file from the route in my node.js + express.js server passing some variable (for example if a user logs in, I would like to greet him). I know it's possible because I've been pointed to jade for that, but I can't find a good example on jade's github about that.
Does anyone of you already solved this and can help me? Thanks.
route:
app.get('/login', function(req, res){
var usr = new User({username: 'myname'})
res.render('login/success', {
title: 'Welcome',
user: usr
});
});
login/success.jade:
h2 Welcome #{user.username}
or any number of other options. see https://github.com/visionmedia/jade for more help with the templating language.
Related
Beginner NodeJs here.
I have created a registration and login page using nodejs and mysql.
It is just a basic setup wherein:
I have a form which submit to nodejs to register (will add a row in mysql database with email and password) and login/authenticate (which will just do a lookup in mysql and search for equivalent email and password).
I can register and login no problem.
The steps I followed are from here:
http://www.expertphp.in/article/user-login-and-registration-using-nodejs-and-mysql-with-example
I have also edited the code a little that if It login successfully, it will redirect to a page via this code..
if (password==results[0].password){
// res.json({
// status:true,
// message: 'successfully authenticated'
// });
var sample = req.body.email;
var newsample = sample.split('#')[0];
return res.redirect('/users/1/?username=' + newsample);
and I also have this route in my users.js
router.get('/1/*', function(req, res, next) {
var q = url.parse(req.url, true).query;
var txt = "Welcome" + q.username;
res.send(txt);
});
it works fine.
What I want to accomplish is that, I can only access the link
http://localhost:3000/users/1/?username=*
if I came from the login page and have authenticated.
Hope you can guide me on how to do about it or a hint on how it is done.
I have search but only found links which uses a different library or authentication which i find hard to understand. Pardon my being noob.
A working example would be very helpful for me.
Thank you.
I think you handle user authentication in a hard way, you can use passport.js it will help you a lot
but you can use the session to save what is necessary to check if the user is logged in like user id and check this session in a custom middleware
i want to create a profile URL in my NodeJs project like facebook
Example: http://localhost:3000/anehkumar
I try a lot but have no luck so please help me. I am using express in this.
Thank you.
You can define dynamicly params in router like below;
app.get('/:username', function(req, res) {
// make somethings with username
var username = req.params.username;
})
I am wondering how to implement the logout function in nodejs using express and express section, the code I wrote is following:
app.get('/logout',function(req, res){
req.session.destroy(function(){
res.redirect('/');
});
});
However, when multiple user login, if one of the user logout, everyone else that login will be log out. Can anyone please tell me how to implement this correctly? thanks :)
NVM, I found out that the problem can be resolve by using the destroy(sid, fn) method in session-memory module
Is there any way using express + swig template for nodeJS to pass variables from the server side to client side javascript? I know it can be done in Jade but I'd rather stick with a template engine that more closely resembles HTML. Thanks for the help guys!
OK I will assume that you could configure your express with consolidate.swig if not please read this link (http://tinyurl.com/kcs8kvy).
Well I didn't find the direct way to pass variable values to client javascript but I have found a workaround.
for instance you're sending an object, in your route at express:
app.get("/", function(req, res){
var myUser = { name: "Obama" };
res.render("index", {user: myUser});
});
then in your index.html file you can create a script tag:
<html>
<body>
<script>
var username = "{{user.name}}";
</script>
<script src="whatever.js"></script>
</body>
</html>
and then in your whatever.js file the username variable will be available with its correct value. I hope that this help you.
To pass json objects from backend server to the template you can do:
var data = {{jsonData|raw|json}}
I have a Node.js server. Lets say each client has his name saved on a variable. They switch page and I want each client to mantain their name on a variable.
This would be very easy with a php form, but I can't see how to do it with Node.js
If I do a form like I would do in php, I manage to send the name to the server:
app.post('/game.html', function(req, res){
var user = req.param('name');
console.log(user);
res.redirect('/game.html');
});
But it seems too complicated to then resend it again to each client it's own.
I just started with Node.js, I guess it's a concept error. Is there any easy way to pass a variable from one page in the client to another?
Thanks.
Instead of redirecting to a static file, you have to render the template ( using any engine that ExpressJS supports ):
app.post('/game.html', function(req, res){
var user = req.param('name');
console.log(user);
res.render( 'game.html', { user:user } );
});
( note that .render requires some additonal settings set on app )
Now user variable becomes available in game.html template.
You can use res.render and pass many variables, like that:
res.render('yourPage', {username:username, age:age, phone:phone});