Trying to view my localhost on my ipad so I don't have to deploy a website first

Viewed 15227

Ok,so this might be an odd question for this forum, but I can't find anything on it anywhere. I am new to web development, I've been at it for about 6 months and I'm slowly getting better. I create localhosts on my comp to view the look of my development for any website I'm building, this way I can see how the site reacts in a real-world setting. I'm positive that most developers do this. What I would like to know is if there is a way to connect an iPad or iPhone to that particular localhost so I can view how responsive my website is before I deploy it? There's got to be some way to view your developing website on a mobile device, I just have no clue on how to do that. So, if anyone is familiar with this, I would really appreciate some advice. Thanks, guys!!

3 Answers

I created a bash function in my .bashrc (or .zshrc) file to do just that:

##
# Bind local server to port to view on mobile device!
# usage (from root location): `railsipserver 3100` (any preferred port)
##
railsipserver() {
  REQUIRED_ARGS_NUMBER="1"

  if [ "$#" -lt "$REQUIRED_ARGS_NUMBER" ]; then
    echo 1>&2 "$0: not enough arguments"
    return
  elif [ "$#" -gt "$REQUIRED_ARGS_NUMBER" ]; then
    echo 1>&2 "$0: too many arguments"
    return
  fi

  PORT=$1
  IP_ADDRESS=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2)
  PID="1$PORT" # needs to be unique to run multiple servers from same location

  echo ""
  echo "~~~~~"
  echo "Your server is starting now!"
  echo "View on external device (phone or iPad) at..."
  echo "http://$IP_ADDRESS/$PORT"
  echo "~~~~~"
  echo ""

  rails server --port $PORT --binding "$IP_ADDRESS" --pid $PID
}

I also created these aliases to run so i can view the same site on my local machine (with localhost:3000) AND simultaneously on my iPad (with http://192.168.X.X:3100).

alias mydir="path/to/my-site-directory"
alias myserver="cd mydir && rails server --port 3000
alias myipserver="cd mydir && railsipserver 3100

So I simply open two tabs in Terminal (or iTerm) and type

Tab 1: myserver

Tab 2: myipserver

Voila! I can view all my local changes on my computer AND on my iPad!

Related