Express-subdomain routing not working correctly? - javascript

I have been looking for solutions for a couple days now trying to google it and all and now i am here. I am trying to setup subdomains for my app using express-subdomain package. However in the example below the app ALWAYS returns app.get route and skips the other subdomain routes specified.
I have also in turn added the hosts file url so i know that should not be the issue.
It must be in my code for some reason it always ends up displaying Detect Region route even when accessing oce.localhost:3000.
Please help me :)
Server.js
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
// Region routes
var router = express.Router();
var na = require('./routes/region/na.js');
var oce = require('./routes/region/oce.js');
router.use(subdomain('na.localhost', na));
router.use(subdomain('oce.localhost', oce));
app.get('/', function(req, res) {
res.send('Detect Region and send to correct subdomain!');
});
app.listen(3000);
routes/region/oce.js
var
express = require('express'),
router = express.Router();
router.get('/', function(req, res) {
res.send('Your are in OCE Region!');
});
module.exports = router;
And na.js is pretty much the name as oce.js
Cheers

You are setting your subdomains in the router variable but you don't tell your app to use it.
You have to do that :
app.use(router);
You put it in place of your current app.get.
Edit
You could also put your app.get after the app.use(router) so that it will act as a default route. (When you are neither on oce or na, it will use it)
Edit after some testing
Alright I've been able to make it work using express-vhost. I just updated your server.js like so :
var subdomain = require('express-vhost');
var express = require('express');
var app = express();
// Region routes
var router = express.Router();
var na = require('./routes/region/na.js');
var oce = require('./routes/region/oce.js');
subdomain.register('na.localhost', na)
subdomain.register('oce.localhost', oce)
app.use(subdomain.vhost(app.enabled('trust proxy')));
app.get('/', function(req, res) {
res.send('Detect Region and send to correct subdomain!');
});
app.listen(3000);

Related

express-subdomain handling any subdomain

I'm trying to use https://github.com/bmullan91/express-subdomain for subdomain routing in express. The following are the contents of my main.js and src/routes/site files.
const express = require('express');
const bodyParser = require('body-parser');
const subdomain = require('express-subdomain');
const siteRouter = require('./src/routes/site');
const app = express()
app.use(express.json() );
app.use(express.urlencoded());
app.use(express.static('public'));
app.use(subdomain('*.www', siteRouter));
app.get('/', function(req, res) {
res.send('Homepage');
});
const server = app.listen(80,'x3.loc', function () {
var host = server.address().address;
var port = server.address().port;
console.log('X3 listening at http://%s:%s', host, port);
});
const express = require('express');
let router = express.Router();
router.get('/', function(req, res) {
res.send('Welcome to site');
});
module.exports = router;
This way of doing app.use(subdomain('*.www', siteRouter)); has been suggested in https://github.com/bmullan91/express-subdomain/issues/33 but does not work.
I have also tried just * as the subdomain aswell, but that caused the homepage w/o a subdomain, to get treated like one aswell. How could I get this to work?
We know that / matches any base path regardless of subdomain. So I made your homepage middleware "subdomain-aware" like so:
app.get('/', function(req, res,next) {
/* If there are any subdomains, skip to next handler, since request is not for the main home page */
if (req.subdomains.length > 0) {
return next();
}
res.send('Homepage');
});
Then I placed the middleware for subdomains below the homepage middleware like so:
app.use(subdomain('*', siteRouter));
This makes homepage middleware to serve requests for x3.loc and the subdomain middleware to serve requests for any subdomain like api.x3.loc or api.v1.x3.loc.
But in my opinion, the real fix should be done in the module. I think it should be changed so that either the case where req.subdomains being empty is handled, or * is matched against an actual string, instead of skipping the iteration.
I am surprised that the fix suggested in bug 33 worked as-is for the reporter. In my testing, it works the opposite way i.e. www.example.com goes to subdomain middleware while stat1.example.com goes to the homepage middleware. Perhaps the reporter saw this and swapped the middleware bodies.

Using multiple router defined in their own files in express

I am new to Node.js and express framework. I am getting an error when I am trying to go to the signuplogin route from my homepage route. I have also tried all the similar questions in the stackoveflow, but none of them have worked for me, so I thought of posting my own.
This is my app.js
var homepage = require('./routes/homepage');
var signupLogin = require('./routes/signuplogin');
var app = express();
app.use("/", homepage);
app.use("/signup-login", signupLogin);
This is my homepage.js in routes directory
var express = require('express');
var homepageRouter = express.Router();
/* GET Homepage. */
homepageRouter.get("/", function(req, res, next) {
res.render('../views/homepage/homepage.hbs');
});
module.exports = homepageRouter;
This is my signup-login.js in routes directory
var express = require('express');
var signupLoginRouter = express.Router();
/* GET Signup Login Page. */
signupLoginRouter.get("/signup-login", function(req, res, next) {
res.send('respond with a resource');
});
module.exports = signupLoginRouter;
Well my "/" route works perfectly but it says 404 when I try to access "/signup-login" route.
I also changed the route to "/signuplogin" because I thought maybe it doesn't except the "-" character in the route. But that didn't work as well. Any solution/advice?
You have done a mistake in your signup-login.js file. Correct the code with this -
/* GET Signup Login Page. */
signupLoginRouter.get("/", function(req, res, next) {
res.send('respond with a resource');
});
As per your code the actual url of signup page becomes to "/signup-login/signup-login"
You don't need to add the singup-login url path again in your page. That is already referenced in your main router.

Why am I getting "name undefined" when trying to post using Express and Body Parser

I'm attempting to build a MEAN app and trying to test POSTing with POSTMAN. When I do, I keep getting the dreaded "TypeError: Cannot read property 'name' of undefined". If I type in a simple string, the POST goes through fine. But when I use "req.body.name" I get the error. I've looked in every place and I'm not seeing my mistake. I even followed the suggestions on this thread with no luck. Any help or suggestions would be greatly appreciated.
Here's the code I am currently working with in my server.js file:
const express = require('express');
var bodyParser = require('body-parser');
var Bear = require('./models/bear')
var path = require('path');
var mongoose = require('mongoose');
var router = express.Router();
var app = express();
var staticAssets = __dirname + '/public';
app.use(express.static(staticAssets));
app.use('/api', router)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Routes for my API
//===================================
// middleware to use for all requests
router.use(function(req,res,next){
// logging happens here
console.log('Something will happen.');
next(); // Head to the next router...don't stop here
});
// Test router to make sure everything is working (accessed at GET http://localhost:3000/api)
router.get('/', function(req, res){
res.json({message: 'hooray! welcome to our api!'})
})
//More routes will happen here with routes that end in "/bears"
router.route('/bears')
//Create a bear (accessed at POST http://localhost:3000/api/bears)
.post(function(req,res){
var bear = new Bear(); // Create a new instance of the bear model
console.log(req);
bear.name = req.body.name; // set the bears name (comes from the request)
//res.send(200, req.body);
bear.save(function(err){
if (err)
res.send(err);
res.json({message: 'Bear Created!!'});
});
});
//======================================
//var Products = require('./products.model.js');
var Product = require('./models/product.model');
var db = 'mongodb://localhost/27017';
mongoose.connect(db);
var server = app.listen(3000);
console.log("App is listening on port 3000");
Thanks.
Also, the url I'm trying to use inside of POSTMAN is http://localhost:3000/api/bears
Express processes requests Top-Down, meaning if you require a piece of functionality to be applied to all routes via middleware, than that middleware needs to be added to your app before any routes that require it. This is usually the case for middleware such as body-parser.
When using Router Middleware, you don't typically construct the router in the same file as the actual Express app that will use it as middleware. Instead, place it in a separate file and/or directory for organization purposes, this is considered a best practice.
Express Apps can be structured like so
/lib
/models
bear.js
product.js
/node_modules
/public
/css
/routes
api.js
package.json
server.js
The routes directory is where you would place any applicable Router Middleware files such as your api router. server.js is your main Express App and public is where your static assets are stored. lib is directory that contains any business logic files and models.
The actual Express app and Router files should look something like this
server.js
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const apiRouter = require('./routes/api');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, public)));
app.use(/api, apiRouter);
app.listen(port, () => {
console.log(`Listening on port ${port});
});
module.exports = app;
routes/api.js
'use strict';
const router = require('express').Router();
const Bear = require('./lib/models/bear');
router.use((req, res, next) => {
// logging happens here
console.log('Something will happen.');
next(); // Head to the next router...don't stop here
});
router.get('/', (req, res) => {
return res.json({ message: 'hooray! welcome to our api!'})
});
router.route('/bears')
//Create a bear (accessed at POST http://localhost:3000/api/bears)
.post((req, res) => {
var bear = new Bear(); // Create a new instance of the bear model
console.log(req);
bear.name = req.body.name; // set the bears name (comes from the request)
//res.send(200, req.body);
bear.save((err) => {
if (err)
return res.send(err);
return res.json({message: 'Bear Created!!'});
});
});
module.exports = router;
To note, you could break up your API even further to increase the amount of decoupling. An example of this would be to move the /api/bear route to its own router middleware and into its own route file. Then simply add it to your routes/api.js router as a middleware like you would in server.js. If your app is going to have a decent sized API, then this would be the best approach because it would allow the most flexibility when it comes to applying middleware to only certain routes and would make maintaining the source much easier.

Node, Express - CANNOT GET route

I am building an Express app and having some issues with routing. My '/' route is working perfectly, however other routes are not. I've looked into other questions people have posted and these have not resolved my issues.
I have a routes/index.js file:
module.exports = function(app){
app.use('/', require('./routes/home'));
app.use('/about', require('./routes/about'));
}
My routes/home.js: - WORKING!
const express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.render('app/home');
});
module.exports = router;
My routes/about.js: - NOT WORKING!
const express = require('express');
const router = express.Router();
router.get('/about', function(req, res) {
res.render('app/about');
});
module.exports = router;
When I go to '/about' I see this error in the browser - 'Cannot GET /about'
Both the home.html and about.html files are located in the same views directory.
Any help here would be very appreciated!
let me quote from express doc:
A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on.
see express doc
this is "not working" because you set the /about in the app.use and in the router.get. try to request /about/about and you will see that this is working (just not as you wanted to)..
now just change the /about in the routes/about.js then rerun and try to request /about and it will work :)
Your route is set to /about/about.
Change about.js to this:
const express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.render('app/about');
});
module.exports = router;

express js routes how to get the root

I'm making a website using angular and when the user hits '/' root i want to go the my www/index.html and render that page, and after that angular can take care of the rest. I just want my application to load that index.html before angular takes over.
my question is how do i make my app initialize, and then i can do the rest with angular.
this is my server.js
// dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
// mongodb
mongoose.connect('mongodb://localhost/test');
// express
var app = express();
//route for the root
app.get('/', function(req, res){
res.render('www/index.html');
});
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
// routes
app.use('/api', require('./routes/api'));
// start server
app.listen(3000);
i have a routes/api.js file that basically takes care of the api calls
// dependencies
var express = require('express');
var router = express.Router();
var app = express();
// models
var User = require('../models/user');
// routes
User.methods(['get', 'put', 'post', 'delete']);
User.register(router, '/users');
// return router
module.exports = router;
If you just want your index.html to be the entry point into your angular app I think you need to serve it as a static asset. Try using express.static and pass it the directory that contains your index.html file. Try something like..
app.use(express.static('./public/'));
app.use('/*', express.static('./public/index.html'));

Categories