NodeJS Routing issue - javascript

how can I route below url to different view in node.js
http://admin.localhost:3000/users/customer/view
and
http://localhost:3000/users/customer/view
currently it go to the same route that I set for
http://localhost:3000/users/customer/view
App.js
....
var users = require('./routes/users');
app.use('/users', users);
....
Users.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/Customer/Create', function(req, res, next) {
res.render('customer', {});
});
router.get('/Customer/View', function(req, res, next) {
res.render('customer', {});
});
router.get('/Employee/Create', function(req, res, next) {
res.render('customer', {});
});
router.get('/Employee/View', function(req, res, next) {
res.render('customer', {});
});
module.exports = router;
and what is the terminology for doing something like this with your url by adding admin before url admin.yoururl.com ?

Since you are using express, you can use the express middleware express-subdomain.
The package even supports multi level subdomains like v1.api.domain.com.
You need to create one Router per subdomain and then bind that Router to your express app using the package:
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
var router = express.Router();
//api specific routes
router.get('/', function(req, res) {
res.send('Welcome to our API!');
});
router.get('/users', function(req, res) {
res.json([
{ name: "Brian" }
]);
});
app.use(subdomain('api', router));
app.listen(3000);

Related

Routing of express-subdomain

I'm using express-subdomain.
The router that handles requests through a subdomain is the same as the router that handles requests without a subdomain.
I know that my 'app.js' setting is wrong.
How can I solve this problem? I want to know a good way. like this:
app.use(subdomain('banana', ('/about', bananaRouter);
and If this is an easy question, please forgive me. I couldn't find any of the same problems in my country. I'm sorry.
// /app.js
const appleRouter = require('./routes/apple/index');
const appleAboutRouter = require('./routes/apple/about');
const applePriceRouter = require('./routes/apple/price');
const bananaRouter = require('./routes/banana/index');
const bananaAboutRouter = require('./routes/banana/about');
const bananaPriceRouter = require('./routes/banana/price');
const grapeRouter = require('./routes/grape/index');
const grapeAboutRouter = require('./routes/grape/about');
const grapePriceRouter = require('./routes/grape/price');
app.use(subdomain('banana', bananaRouter));
app.use(subdomain('grape', grapeRouter));
app.use('/', appleRouter);
app.use('/about', appleAboutRouter);
app.use('/price', applePriceRouter);
app.use('/', bananaRouter);
app.use('/about', bananaAboutRouter);
app.use('/price', bananaPriceRouter);
app.use('/', grapeRouter);
app.use('/about', grapeAboutRouter);
app.use('/price', grapePriceRouter);
// /routes/apple/index
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.send('I am Apple');
});
module.exports = router;
// /routes/apple/about
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("Apples don't taste good.");
});
module.exports = router;
// /routes/apple/price
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("$ 1");
});
module.exports = router;
// /routes/banana/index
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.send('I am Banana');
});
module.exports = router;
// /routes/banana/about
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("Bananas are delicious.");
});
module.exports = router;
// /routes/banana/price
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("$ 2");
});
module.exports = router;
// /routes/grape/index
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.send('I am Grape');
});
module.exports = router;
// /routes/grape/about
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("Grapes are purple.");
});
module.exports = router;
// /routes/grape/price
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("$ 3");
});
module.exports = router;
Expected Behaviour 2:
// http://localhost.com:3000/
I am Apple
// http://localhost.com:3000/about/view
Apples don't taste good.
// http://localhost.com:3000/price/view
$ 1
// http://banana.localhost.com:3000/
I am Banana
// http://banana.localhost.com:3000/about/view
Apples don't taste good. ** not Bananas are delicious. **
// http://banana.localhost.com:3000/price/view
'$ 1' ** not '$ 2' **
I created code sample which resolve you problem :D Firstly you need to add a couple lines to your /etc/hosts file. Example
127.0.0.1 banana.myapp.dev
127.0.0.1 myapp.dev
And after that try to run this script which I wrote for you:
//connect express
var express = require('express');
var subdomain = require('express-subdomain');
var app = express();
app.use(express.json());
//set sub routing
app.sub_banana = express.Router();
app.use(subdomain('banana', app.sub_banana));
//top level routing
app.get('/', (req, res) => {
res.send('I am Apple')
});
app.get('/about', (req, res) => {
res.send('Apples don\'t taste good.')
});
//subdomain routing
app.sub_banana.get('/', (req, res) => {
res.send('I am Banana')
});
app.sub_banana.get('/about', (req, res) => {
res.send('Apples don\'t taste good. ** not Bananas are delicious. **')
});
//start server
var http = require('http');
var port = 3000
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
Let me know if you need more information. I got my answer from this resource https://exceed-team.com/tech/express-subdomain

How do I reroute a GET request from my root in app.js to a route in my controllers folder in Express.js?

I'm basically trying to recreate this from Sinatra in Express:
get '/' do
redirect '/channels'
end
I'm trying to build a Node.js/Express.js app and am starting to incorporate an MVC structure. My app.js file contains my / route, as such:
app.js
app.get('/', function(req, res) {
res.redirect('/search');
})
I want it to redirect to the /search route in controllers/search.js, which contains the following:
controllers/search.js
const express = require('express');
const app = express();
app.get('/search', function(req, res) {
res.render('index.js');
})
The browser does redirect to localhost:3000/search but it displays Cannot GET /search. All of the tutorials and documentation I see about rerouting in Express don't show the whole file so I'm not able to tell if I have to require or export anything ala Node.js.
Any help is appreciated.
try this
server.js
var http = require('http');
var express = require('express');
var searchRouter = require('./searchRouter');
var app = express();
app.use('/', searchRouter);
var server = http.createServer(app);
server.listen(3000);
searchRouter.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.send('Nope, try /search');
});
router.get('/search', function(req, res, next) {
res.send('yeah!! you found me');
});
module.exports = router;
you can extend this logic by
app.use('/search', searchRouter);
in search router
// this handles /search
router.get('/', function(req, res, next) {}
//this handles /search/apple
router.get('/:id', function(req, res, next) {}

Express subrouter returns 404

I have this router (http/api/ping.js):
var express = require('express');
var router = express.Router();
router.get('/ping', function (req, res) {
res.send("You called /api/ping");
});
module.exports = router;
This router is embedded into this router (http/api/index.js):
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.send('You called /api');
});
router.use('/ping', require('./ping'));
module.exports = router;
And this router is used by my Express.js app (app.js):
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('./config').logger;
// Create app
var app = express();
var server = http.createServer(app)
var io = require('socket.io')(server);
// App config
app.use(bodyParser.json());
app.use('/api', require('./http/api'));
// Display requests on console
app.use(function (req, res, next) {
logger.trace(req.method, req._parsedUrl.href);
next()
});
module.exports = {
app: app,
server: server
};
When I run the app, /api returns You called /api, but /api/ping gives me a 404.
I am using Node 6.9.1 and Express ^4.14.0
I think order matters in this scenario. Try putting /ping above the / get route.
router.use('/ping', require('./ping'));
router.get('/', function (req, res) {
res.send('You called /api');
});
Also in your ping route you say the path to your route is /ping you also say it is /ping when you import it to the other router, which would make the path /api/ping/ping
change
router.get('/ping', function (req, res) {
res.send("You called /api/ping");
});
to
router.get('/', function (req, res) {
res.send("You called /api/ping");
});
I think your routing is incorrect on this line
router.use('/ping', require('./ping'));
this will point to http/api/ping/ping
it should be
router.use('/', require('./ping'));

Failed to pass value between middlewares

I got this in app.js
var routes = require('./routes/index');
app.use(function(req,res,next) {
res.something = 'something_value';
next();
}
});
app.use('/', routes);
Then I have a routes folder, it has index.js I do
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
console.log(req.something)
});
but I got undefined. What's wrong?
I also tried req.body.something, I got undefined too.

Router.use requires middleware function?

So I'm trying to seperate my login routes in a seperate JS file called login_routes.js
I keep getting this specific error:
TypeError: Router.use() requires middleware function but got a Object
at Function. (/Users/ethanthomas/Desktop/mean-stuff/express-server/node_modules/express/lib/router/index.js:446:13)
Not entirely understanding what it's asking me to implement?
login_routes.js:
var express = require('express');
var app = express();
app.route('/login')
.get(function(req, res, next) {
res.send('this is the login form');
})
.post(function(req, res, next) {
console.log('processing');
res.send('proccessing the login form!');
});
server.js:
var express = require('express');
var app = express();
var path = require('path');
var adminRoutes = require('./app/routes/admin_routes');
var loginRoutes = require('./app/routes/login_routes');
app.use('/admin', adminRoutes);
app.use('/login', loginRoutes);
//send our index.html file to the user for the home page
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
//start the server
app.listen(1337);
console.log('leet is the magic port');
Your login_routes.js should look something like this (in the context of express):
var express = require('express');
var router = express.Router();
// GET request to /login
router.get('/', function(req, res, next) {
// do something
});
// POST request to /login
router.post('/', function(req, res, next) {
// do something
});
module.exports = router;
In your app.js you use
var login_routes = require('./login_routes');
...
app.use('/login', login_routes);
...
Have a look at the code generated by the express-generator module. That is a starting point for express webserver apps.
People have already offered hints at the solution in comments.
The first issue is that you need to export your "sub" app from login_routes.js. To do so, change this:
var app = express();
Into this:
var app = module.exports = express();
Secondly, you are—probably unintentionally—creating routes for GET /login/login and POST /login/login. To solve this, use this in login_routes.js:
app.route('/').get(...).post(...);
This is because the root path in your sub app (the one in login_routes.js) will get mapped to the path used in app.use() in your main app (server.js).
Do like this:
login_routes.js:
exports.get = function( req, res ) {
res.send('this is the login form');
};
exports.post = function( req, res ) {
console.log('processing');
res.send('proccessing the login form!');
};
server.js:
var loginRoutes = require('./app/routes/login_routes');
app.get('/login', loginRoutes.get);
app.put('/login', loginRoutes.post);
login_routes.js:
var express = require('express');
var app = express();
app.route('/login')
.get(function(req, res, next) {
res.send('this is the login form');
})
.post(function(req, res, next) {
console.log('processing');
res.send('proccessing the login form!');
});
module.exports = router;
just writ module.exports = router then it will be work

Categories

Resources