Search code examples
node.jsbase-url

How can i change the base Url in Meanapp or Nodejs app based on Environment?


Ex:-
i run an app locally with API(returns json)

http://localhost:8000/userslist -(returns json)

what plugin can i use for changing the baseurl in Mean App?

Pls Suggest a plugin for now iam using some code to start the server:-

 app.set('port',process.env.PORT||8000)
//console.log('Your app running on Environment :-',process.env.PORT);

var server=http.createServer(app).listen(app.get('port'),function(){
    console.log('express server listening on port'+app.get('port'));
});

but i think it may not produce the required result.

Example:- now iam calling the api http.get(baseurl+'user/1') where baseurl='http://localhost:8000'. when the app deployed to the dev server it should rewrite the baseurl with dev string .. http://dev.mydomain.com instead of localhost:8000 in the same way for the Production. and sorry for confusion in question .

Thanks For the Help in Advance !!


Solution

  • If by "baseurl", you mean the domain . . . you can't really change that. It will either be on localhost or on the domain of whatever server you set up. Unless you add an entry to /etc/hosts pointing localhost to some other domain. If you mean part of the path . . . that's what express is for (the "E" in "MEAN"). Just:

    var express = require('express');
    var app = express();
    app.get('/userlist', function() { // get users from Mongo });
    

    If you mean something other than one of those things, then you need to clarify your question. Maybe give an example of the desired output/effect.

    EDIT:

    Oh, are you wanting to point to a different port based on the environment?

    EDIT 2:

    Based on your comments, you don't need configuration for this. Most of the time, you'll just want to use relative paths and let the server figure the domain parts out. E.g., don't redirect to "my.specialdomain.com/foo." Just redirect to "/foo" and Express/the server/the browser will do the rest. Same with client side links - just use relative paths:

    <a href="/foo/bar">Blah</a>
    

    Same with requests from the client. They understand relative paths, so you can (most of the time) use something like http.get('/foo/bar') (not sure what library you're using, so adjust as necessary).

    If for some reason, you need to include the domain - like if you want to redirect from http to https - you can get it from the Host header. With express, just use req.hostname. That will give you the domain minus the port. Then you can selectively include a port if necessary. If you need to do that often, you might think about using a special middleware function to set that up. For example:

    app.use(function(req, res, next) {
      // Or maybe a switch on process.env.NODE_ENV if you have more than 2 environments
      req.hostWithPort = req.hostname + (process.env.NODE_ENV === 'production' ? '' : ':' + app.get('port'));
      next();
    });
    

    and then use it wherever you need it, e.g.:

    res.redirect(301, 'https://' + req.hostWithPort);