We're making our first node.js Q&A app and we are using passport.js as a register/login system.
I'm wondering if it's possible to acces data from the logged in user at a view?
For Example:
User registers
User logs in
User get's redirected to a list of questions (www.dev.com/topics) and on this page we want to show the
users First Name
What do we need to do to make this happen?
We've tried several things but nothing seems to work.
Here's my topic router
var express = require('express');
var router = express.Router();
var Topic = require('../models/topicModel.js');
router.get('/', function(req, res, next) {
res.render('create_topic.ejs', { title: 'Topics', user: req.user });
});
router.get('/id/:id', function(req, res, next) {
var id = req.params.id;
Topic.findOne({topicID: id}, function(err, doc){
res.render('questions.ejs', {docs: doc});
});
});
module.exports = router;
Related
I want to create a login page, such that when the user successfully logs in the app goes to the URL localhost:3000/:username. So for instance when a person with username johnlogs in the app should go to localhost:3000/john. I read this page on express routing https://expressjs.com/en/guide/routing.html but it does not seem to explain this. It only explains how to get the username from the URL but not the other way around (how to route to a different URL depending on the username). Any hints would be appreciated.
You can use redirection after the user login successfully.
var express = require('express');
var app = express();
// login page
app.get('/login', function (req, res) {
res.send(`This is login page`);
});
// login verification
app.post('/login', function (req, res) {
// verify user
const user = getUserByEmailAndPassword(req.body.email, req.body.password);
// set session
// ...
// redirect
res.redirect(`/${user.username}`);
});
// private user page
app.get('/:username', function (req, res) {
const username = req.params.username;
res.send(`Hello ${username}`);
});
This question already has answers here:
res.redirect doesn't redirect Node/Express.js
(2 answers)
Closed 4 years ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I am building my first app in Node.js. Everything was going fine until now. I am facing a problem while trying to redirect using server-side code in Node.js application doesn't throw any error, but on the browser it stays on the same page and doesn't redirect to chatroom page from the index page.
Index.js
var express = require('express');
var router = express.Router();
var obj = require('../bin/www');
var db = require('../databaseModels');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express Chat' });
});
//here we will save the username of the New user
router.post("/setUserName", async function(req,res) {
try{
var newUser = new db.Users(req.body);
await newUser.save()
//Emit the event that a new user has joined the chat
obj.io.emit("newUserJoined", req.body)
return res.redirect(301,"http://192.168.34.54:4747/chatroom");
}
catch(error) {
res.sendStatus(500);
console.log(error);
}
})
module.exports = router;
chatroom.js
var express = require('express');
var router = express.Router();
var obj = require('../bin/www');
var db = require('../databaseModels');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.render('chatroom', {title: 'Welcome to Chat Room'});
});
module.exports = router;
Use your 'chatroom' router in bin/www like you have used 'index' router.
app.use('/chatroom',chatroom);
and in index router, use relative paths while calling the chatroom router.
return res.redirect(301,"/chatroom");
I'm trying to, after create an account, login the user automatcally.
Need to redirect them to /auth/local/ send the user and password that he just created.
This is my middleware/signup.js
'use strict';
module.exports = function(app) {
return function(req, res, next) {
const body = req.body;
// Get the user service and `create` a new user
app.service('users').create({
email: body.email,
password: body.password
})
// Then redirect to the login page
.then(user => app.post('/auth/local', function(req, res){
req.body(user);
}))
// On errors, just call our error middleware
.catch(next);
};
};
This dont give any errors...Just a eternal loading after creating an User.
Help
First of all it is an eternal loading because you are not sending a response or calling next. Please read about express middlewares here.
Besides that, why do you want to make a POST request to your own API from itself? I don't have enough context here about what app is but your code looks more like a route definition, than like a request.
Here you should have some way to call a function that authenticates the user.
For example if using passport:
'use strict';
module.exports = function(app) {
return function(req, res, next) {
const body = req.body;
// Get the user service and `create` a new user
app.service('users').create({
email: body.email,
password: body.password
})
// Then redirect to the login page
.then(user => passport.authenticate('local')(req, res, next))
// On errors, just call our error middleware
.catch(next);
};
};
I have 2 router files. One is for view routing and other for api requests.
I am trying to set the routing using:
var routes = require('./routes/index'); //View Router
var api = require('./routes/api'); //API Router
app.use('/', routes);
app.use('/api', api);
This fails in case of /api requests. If I remove one of the routings, the other works.
I also tried,
routes(app);
api(app);
But this fails too. Any idea what might be the issue? Please let me know in case additional details are required.
routers/index.js
var express = require('express');
var router = express.Router();
var Promise = require('bluebird');
var nforce = require('nforce');
var org = require('../lib/connection');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'App' });
});
router.get('/accounts', function(req, res, next){
console.log(org);
res.render('partials/' + name);
org.query({query: 'Select Id, Name, Type, Industry, Rating From Account Order By LastModifiedDate DESC'})
.then(function(results){
console.log(results);
res.render('accounts', {title: 'Accounts', records: results.records});
});
});
router.get('/partials/:name', function(req, res, next){
var name = req.params.name;
console.log(name);
res.render('partials/' + name);
});
router.get('/api/:name', function(req, res, next){
var name = req.params.name;
console.log(name);
res.render('api/' + name);
});
module.exports = router;
/routers/api.js
var express = require('express');
var router = express.Router();
var Promise = require('bluebird');
var nforce = require('nforce');
var org = require('../lib/connection');
/* GET home page. */
router.get('/getAccounts', function(req, res, next) {
console.log('in API router...');
org.query({query: 'Select Id, Name, Type, Industry, Rating From Account Order By LastModifiedDate DESC'})
.then(function(results){
console.log(results);
res.json({'accounts': results.records});
});
});
module.exports = router;
Maybe wrong but i see problem in this route.
router.get('/api/:name', function(req, res, next){})
It will match /api/cuteName and it also will match /api/getAccounts.
So you need to make routes more clear. I would suggest to change route inside index.js to be all something like /main/.
And all API routes move to api.js.
Hope this helps.
I'm creating an app using Node and Express. However, I can see it'll soon become difficult to manage all the routes that are placed inside app.js. I have placed all my models in a subdirectory /models.
Here's my app current structure:
app.js
models
-- products
-- customers
-- ...
public
views
node_modules
In app.js:
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path'),
EmployeeProvider = require('./models/employeeprovider').EmployeeProvider,
Products = require('./models/products').Products,
Orders = require('./models/orders').Orders,
Customers = require('./models/customers').Customers,
checkAuth = function(req, res, next) {
if (!req.session.user_id) {
res.send('You are not authorized to view this page');
} else {
next();
}
};
var app = express();
Then some configuration like port, views directory, rendering engine, etc.
Further down app.js I've got the routes:
app.get('/product/edit', auth, function(req, res) {
Products.findAll(function(error, prds) {
res.render('product_edit', {
title: 'New Product',
products: prds
});
});
});
At the top I'm assigning the contents of models/products.js to a variable, all works fine. However keeping all routes inside app.js is not ideal. But if I move the routes to routes/product.js and load the Products models:
var prod = require('../models/products.js');
I get an error saying that object has no method findAll.
What am I doing wrong? How can I remove the routes from app.js?
As of express 4.x Router is added to support your case.
A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.
Example from expressjs site:
// routes/calendarRouter.js
var express = require('express');
var router = express.Router();
// invoked for any requested passed to this router
router.use(function(req, res, next) {
// .. some logic here .. like any other middleware
next();
});
// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function(req, res, next) {
// ..
});
module.exports = router;
Then in app.js:
// skipping part that sets up app
var calendarRouter = require('./routes/calendarRouter');
// only requests to /calendar/* will be sent to our "router"
app.use('/calendar', calendarRouter);
// rest of logic
Since I don't like repetition, here's what I do:
// app.js
//...
var routes = requireDir('./routes'); // https://www.npmjs.org/package/require-dir
for (var i in routes) app.use('/', routes[i]);
//...
And each file in routes is like:
// routes/someroute.js
var express = require('express');
var router = express.Router();
router.get('/someroute', function(req, res) {
res.render('someview', {});
});
module.exports = router;
This way you can avoid long repetitive lists like this one:
app.use('/' , require('./routes/index'));
app.use('/repetition' , require('./routes/repetition'));
app.use('/is' , require('./routes/is'));
app.use('/so' , require('./routes/so'));
app.use('/damn' , require('./routes/damn'));
app.use('/boring' , require('./routes/boring'));
Edit: these long examples assume each route file contains something like the following:
var router = express.Router();
router.get('/', function(req, res, next) {
// ...
next();
});
module.exports = router;
All of them could be mounted to "root", but what for them is "root", is actually a specific path given to app.use, because you can mount routes into specific paths (this also supports things like specifying the path with a regex, Express is quite neat in regards to what you can do with routing).
I can suggest you this file structure (according to Modular web applications with Node.js and Express from tjholowaychuk):
app.js
modules
users
index.js
model.js
users-api
index.js
static-pages
index.js
user-api and static-pages export expressjs applications, you can easily mount them in app.js.
In users module you can describe some Data Access operations and all methods about manipulating with the User entity (like create, update etc.). Our API module will use all these methods.
And here is sample code of app.js file (without common express stuff, only mounting routes from different modules):
var express = require('express');
var app = express();
// mount all the applications
app.use('/api/v1', require("user-api"));
app.use(require("static-pages"));
app.listen(3000);
To use your modules this way you must start your app like this NODE_PATH=modules node app.js (i put this line to package.json file in scripts section).
Here is sample code of users module:
index.js
User = require("./model");
module.exports = {
get: function(id, callback) {
User.findOne(id, function(err, user) {
callback(err, user);
});
},
create: function(data, callback) {
// do whatever with incoming data here
data = modifyDataInSomeWay(data);
var newUser = new User(data);
newUser.save(function(err, savedUser) {
// some logic here
callback(err, savedUser);
});
}
};
model.js (with Mongoose stuff for example of course!)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = new Schema({
firstname : {type: String, required: false},
lastname : {type: String, required: false},
email : {type: String, required: true}
});
module.exports = mongoose.model('user', User);
And example of user-api module (here is the main part of the answer about separating routes and models).
var users = require("users");
var express = require("express");
var app = module.exports = express(); // we export new express app here!
app.post('/users', function(req, res, next) {
// try to use high-level calls here
// if you want something complex just create another special module for this
users.create(req.body, function(err, user) {
if(err) return next(err); // do something on error
res.json(user); // return user json if ok
});
});
And example of static-pages. If you are not going to build a kind of REST interface you may simply create several modules that will render pages only.
var express = require("express");
var app = module.exports = express(); // we export new express app here again!
app.get('/', function(req, res, next) {
res.render('index', {user: req.user});
});
app.get('/about', function(req, res, next) {
// get data somewhere and put it in the template
res.render('about', {data: data});
});
Of course you can do whatever you want with modules. The main idea about expressjs is to use a lot of small apps instead of single one.
About nodejs modules you can read stackoverflow and docs.
Hope this helps.