Search code examples
expresslocalhost

I want to turn my localhost server into a real website


I created an application that runs on a localhost server using expressjs. And I also bought a domain.

I'm wondering if there is a way to take that localhost server and turn it into a real shared server

I tried once to use a hosting service like hostgator but I still don't know how I can turn the express app into a real website.

I have no experience with any web development services so please don't tell me to use ....... whatever because I will have no idea what that is.


Solution

  • For one thing it is not clear how your website actually works: if it is only express does it generate HTML or is it purely JSON passed to browser clients via get requests (to each their own).

    There are so many options as to how you might do this: one of the best options is to first make sure your server runs on Docker. Find a tutorial on YouTube/google/Stack Overflow/Blogs on how to run your Express server with docker. If you do that you can deploy it to a Container manager like Google/Amazon/Digital Ocean. If this seems hard to you there are other options.

    Presumably you run your server with something like npm start. This guide can show you how to do essentially that but on a cloud computer.

    Before you begin make sure that you're locally working server is checked in to a cloud Git provider like Github, GitLab, Bitbucket, etc.

    Since Amazon AWS, and Google Cloud have free tier or options for hosting for free for a certain amount of time (AWS 1 year) or for a certain amount of money (Google Cloud). These two seem like viable place to start.

    If you find the option that you'd like you'll need to:

    1. create an account
    2. Create a server (choose a cheap one especially initially like micro/small/cheap etc).
    3. Find a tutorial on how to "SSH" into that server (which basically means remotely control the terminal on that server). Google actually makes this fairly easy there's a big button that says SSH into this server.
    4. Once you've logged into that Computer you'll be able to run the same commands you probably normally do on your home computer:
    5. The computer you'll be getting is likely to be a virtual Linux Computer probably something like Linux Ubuntu. Find a tutorial on how to install git and node on Ubuntu (but it's something like sudo apt-get update && sudo apt-get install git node).
    6. Once you have git and node try mkdir www and cd into that: mkdir www && cd www (This isn't critical but conventional.)
    7. Copy the link that allows you to "Clone your repo using HTTPS" (there's a link at the top right of your GitHub (or others) repo that allows you to do that. You'll need to enter your password
    8. Now all the files that you had on your computer within that repo are on this new AWS EC2 or "Compute Engine" computer.
    9. Next you'll have to probably npm i to install your dependent NPM packages. (This assumes you properly used .gitignore to prevent GitHub from being filled with extra copies of your npm packages.)
    10. Now you should be able to run your code as usual: npm run start

    If all those steps work you'll want to get something that will run these "forever" like https://www.npmjs.com/package/forever npm i -g forever or even better: https://www.npmjs.com/package/pm2 will allow you to continuously run your express server.

    Finally, you'll need to configure this server on AWS/Google/whatever service you're using to push traffic coming in on port 80 and 443 to the port where your express server is running (often port 3000) and open traffic to all. And depending on the service you chose that's different so find a tutorial for doing just that part.

    This will only allow people across the internet to see your service on an AWS URL or a google URL. But it's a good chance to make sure everything works perfectly. Once you're happy with everything associate your purchased domain with that special AWS/Google domain. You can do that on the AWS side, or the GoDaddy/NameCheap/where-ever you bought your domain side.

    For the docker option you can download aws-cli tools and upload your built docker container to AWS and have it available. Find a tutorial to do that.

    Essentially your question is very broad so I sometimes brushed over some details, but this is essentially what you have to do.