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;
})
Related
so im trying to make some sort of minecraft api that grabs information from a database
Im trying to do something like Mojang where there api is
/session/minecraft/profile/{uuid}
I want to be able to do something similar with express where depending on the uuid or username provided in the url, it would return different information without passing a json body or extra web arguments.
Basically, I want to catch the UUID out of the URL similar to how mojang does it
My url will be something like /status/profile/{username}
Thanks
Figured it out
app.get("/stats/profile/:uusername", (req, res, next) => {
var useruuid = req.params.uuid;
console.log(useruuid);
res.sendStatus(204);
});
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'm running this sample application of Stormpath Express Passport
https://github.com/stormpath/stormpath-passport-express-sample
I can't wrap my head around how can you post a form and save customData or even the default data like the given name and surname. Overall I would appeciate if I could get a HTML(Jade or EJS) example with lines I need to write in app.js or index.js. I've been struggling with this for 4 evenings.
I've tried multiple tutorials for Express, Stormpath, but none of them are meant to work with this sample application. I always had to install multiple new modules, but even that didn't get anything to work.
I've would highly appreciate if someone from Stormpath or really anyone who has experience with it, would help me with my problem and perhaps update their tutorials on the website as well, because some seem to be outdated.
I wrote that article and project, and I'm also the author of express-stormpath (our new library which makes this stuff much easier).
The simplest way to do this is in a route (using our passport library) is like so (note, you might need to upgrade to the latest version of our Node library to make this work):
req.user.getCustomData(function(err, data) {
data.someValue = 'blah';
data.someOtherValue = {
woot: 'hi',
nope: 'there',
};
data.save(); // persist this data to Stormpath
res.send('done!');
});
If you're using our Express library, you can do this a bit easier, here's an example:
var express = require('express');
var stormpath = require('express-stormpath');
var app = express();
app.use(stormpath.init(app, {
expandCustomData: true,
}));
app.get('/data', stormpath.loginRequired, function(req, res) {
req.user.customData.color = 'black';
req.user.save();
res.send('stored data!');
});
app.listen(3000);
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});
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.