I'm making an app the will let different users see the different root page. Let's say there are two groups: admin and viewer. What I want to do is like:
var admin = require('./routes/admin');
var viewer = require('./routes/viewer');
app.get('/', function(req,res,...) {
if (req is admin) app.use('/', admin);
else app.use('/', viewer);
});
Is this possible, or I should redirect them to different paths? Thanks!
This needs to be done by redirecting a route to other route according to the requirement.
This can be done using below code:
var express = require('express');
var app = new express();
app.get('/', function(req, res, next) {
if(true) { //group check
res.redirect('admin');
} else {
res.redirect('viewer')
}
});
app.use('/admin', function(req, res, next) {
res.send('admin')
});
app.use('/viewer', function(req, res, next) {
res.send('viewer')
});
app.listen(3000);
Related
I'm trying to export a router 'Accounts' to use in my app. The 'Accounts' router has the paths '/login' (POST), '/register'(POST), 'login' (GET), and '/logout' (POST). In my index app I am using the router with the path '/account'. So the paths should be:
/account/login (POST)
/account/login (GET)
/account/register(POST)
/account/logout (GET)
But when I call these paths they aren't found by the app:
How do I get the paths in the 'accounts.js' router to work in the 'index.js' app?
My file structure is like this:
my account.js file looks like this:
const express = require('express');
const passport = require('passport');
const Account = require('../models/Account');
const Branch = require('../models/Branch')
const router = express.Router({mergeParams: true});
const registerAccount = (req, res, next) => {
//register the account
};
const createUser = (req,res) => {
//create a user in another db
}
router.post('/register',
[registerAccount, createUser]);
router.get('/login', function(req, res) {
res.json(user);
});
router.post('/login', passport.authenticate('local', { successRedirect: '/',
failureRedirect: 'account/login' }));
router.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
});
module.exports=router;
and my index.js looks like this:
// index.js
var express = require("express");
var bodyParser = require("body-parser");
var jwt = require("jwt-simple");
var auth = require("../auth/auth.js")();
var users = require("./users.js");
var cfg = require("../config.js");
const accountController = require('./account');
var app = express();
app.use(bodyParser.json());
app.use(auth.initialize());
app.use('/account',accountController);
app.get("/", function(req, res) {
res.json({
status: "My API is alive!"
});
});
app.post("/token", function(req, res) {
//some token stuff that doesn't matter here
});
module.exports = app;
For starters, you don't pass an array to a router.post(), so change this:
router.post('/register', [registerAccount, createUser]);
to this:
router.post('/register', registerAccount, createUser);
And make sure that registerAccount calls next() when it's done and wants createUser() to get called.
In the doc, for this syntax:
app.post(path, callback [, callback ...])
the brackets in [, callback] mean that parameter is optional. The brackets are not supposed to be used.
Can anyone help me figure out why my express routes arent working please. I have been stuck for days trying to figure this out but I keep getting 404 errors when I got to any of the routes in customer.js
<--------------------main server-------------->
var express = require("express");
var path = require('path');
var app = express();
var indexRouter = require('./routes/index');
var ordersRouter = require('./routes/orders');
var customersRouter = require('./routes/customers');
var productsRouter = require('./routes/products');
app.use('/', indexRouter);
app.use('/customers', customersRouter);
app.use('/orders', ordersRouter);
app.use('/products', productsRouter);
app.listen(80);
module.exports = app;
one of my route files below the only route file works is the index router.
var express = require('express');
var router = express.Router();
router.get('/customers', function(req, res, next) {
console.log(req.status())
res.send('<p>some html</p>');
});
router.post('/customers/update/shipping', function(req, res) {
res.send('<p>someg html</p>');
});
router.post('/customers/update/billing', function(req, res) {
res.send('<p>somew html</p>');
});
router.post('/customers/update/cart', function(req, res) {
res.send('<p>some dhtml</p>');
});
module.exports = router;
<------------------------------------------------------>
In your customers files your routes will already be relative to /customers. remove the leading /customers from all your routes. For e.g:
router.get('/customers', function(req, res, next) {
console.log(req.status())
res.send('<p>some html</p>');
});
Should just be
router.get('/', function(req, res, next) {
console.log(req.status())
res.send('<p>some html</p>');
});
same principle applies for the other files.
Can someone help me why default route is not working in my Mean App, But the next routing works
Here when I open http://localhost:3000 I am not able to see any output, But I have defined route in route.js which is working
var express = require('express');
var cors = require('cors');
var bodyparser = require('body-parser');
var mongoose = require('mongoose');
var path = require('path');
const port = 3000;
app.get('/', function (req, res) {
res.send('Test');
console.log('Opened the root path');
});
When I open the page with http://localhost:3000/main I am able to see the Output and also log written in the console
const express = require('express');
const router = express.Router();
router.get('/main', function (req, res, next) {
res.send('This is the Admin Landing Page');
});
router.get('/install', function (req, res, next) {
res.send('This is the Install Landing Page');
console.log('Opened the Install path');
});
module.exports = router;
It looks like you the code you pasted is the full version, and it's not runnable because:
You did not declare app variable.
You did not start the http server.
It's really hard to tell the root cause what's wrong of your code. Following codes works for me:
const express = require('express');
const port = 3000;
let app = express();
app.get('/', function (req, res) {
res.send('Test');
console.log('Opened the root path');
});
let server = require('http').createServer(app);
server.listen(port, function() {
console.log('Server started');
});
So i have been racking my brain for quite awhile now trying to figure this out and I'm still having issues.
So i define a route like this:
var home = require('./routes/home');
var app = express();
app.use('/home/:client', home);
And my home.js code looks like this:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('homeview', { title: 'Welcome',
user: username});
});
router.get('/:client', function(req, res, next) {
var theClient = req.params.client;
console.log(theClient)
});
module.exports = router;
And then when i try to go to this URL:
http://www.localhost:3000/home/client
Nothing happens. For some reason it never gets to the second router.get and never logs anything. Anyone notice whats wrong?
Working solution # https://github.com/rmjoia/expressRoutes
app.js
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
homeRouter = require('./home');
app.use('/home', homeRouter);
app.route('/')
.all(function (request, response, next) {
// Do some plumbing stuff
next();
})
.get(function (request, response, next) {
response.send('OK GET - Hello Stack Overflow');
next();
});
app.listen(port, function (error) {
if (error) {
console.error(error.message);
}
else {
console.info('Server up and running. Listening on port ' + port);
}
})
home.js
var express = require('express');
var homeRouter = express.Router();
var router = (function (router) {
// define the home page route
router.get('/', function (req, res) {
res.send('home route - homepage');
});
// define the about route
router.get('/:client', function (req, res) {
res.send(req.params.client);
});
return homeRouter;
})(homeRouter);
module.exports = router;
I am relatively new to MEANJS, i have also been reading through its documentation to learn more.
so, i am trying to create a rest API using meanjs using its yo generator and removing the angular parts of it, so far it has been a success.
What i am trying to do now is to default the url routes to have a prefix of /api/:version, what i did so far is to append the /api/:version to the routes inside app/ like the following
//app/routes/articles.server.routes.js
'use strict';
...
module.exports = function(app) {
// Article Routes
app.route('/api/:version/articles')
...
app.route('/api/:version/articles/:articleId')
...
// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
};
That works so far, but can i declared this somewhere so that i don't have to add /api/:version every time i create a new route?
I tried implementing the ones stated in express documentation and Remi M's answer in this stackoverflow question with no luck.
Update
meanjs installation comes with an express.js, so i thought that this is the right place to do it and i tried using the router object to solve this problem, although it doesn't work
//config/express.js
'use strict';
...
var express = require('express');
...
var router = express.Router();
module.exports = function(db) {
var app = express();
...
app.use(function(req, res, next) {
res.locals.url = req.protocol + '://' + req.headers.host + req.url;
next();
});
app.use(compress({
filter: function(req, res) {
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
},
level: 9
}));
...
config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
require(path.resolve(routePath))(app);
});
app.use(function(err, req, res, next) {
if (!err) return next();
console.error(err.stack);
res.status(500).render('500', {
error: err.stack
});
});
app.use(function(req, res) {
res.status(404).render('404', {
url: req.originalUrl,
error: 'Not Found'
});
});
...
app.use('/api/:version', router); //this is what i add
return app;
};
All of your initial configurations should be on router object and not on app For Ex :
'use strict';
var express = require('express');
var router = express.Router();
module.exports = function(db) {
var app = express();
router.use(function(req, res, next) {
res.locals.url = req.protocol + '://' + req.headers.host + req.url;
next();
});
router.use(compress({
filter: function(req, res) {
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
},
level: 9
}));
config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
require(path.resolve(routePath))(router);
});
router.use(function(err, req, res, next) {
if (!err) return next();
console.error(err.stack);
res.status(500).render('500', {
error: err.stack
});
});
router.use('/bar', function(req, res, next) {
console.log("bar endpoint called");
res.send("bar");
});
router.use(function(req, res) {
res.status(404).render('404', {
url: req.originalUrl,
error: 'Not Found'
});
});
app.use('/api/:version/', router); //this is what i add
return app;
};
Then call app.use("prefixurl",router) as above.
test the BAR API /api/v1/bar