Running a web server on a Mac - Main image

Intermediate

Running a web server on a Mac

Here we are going to show you how to run a web server on your mac using Node JS.

Žiga Triller - CodeBrainer
Žiga Triller
3 min read

This is what happened - you just started learning Web development, you covered all the basics and are now beginning to learn a high-level programming language like for example JavaScriprt. The problem is that you have written your first lines in Javascript and connected your HTML index file with your JS file, but you don't know how to test it. You can find a web hosting site but usually, they are not for free, and it is a waste of time if you only want to test your website. There are a couple of alternatives, but my favourite one is the http server module which you can use with Node JS.

Are you a Windows user?

Running a Web Server on Windows

This is a solution to:

  • Running a website on your Mac
  • Running a website without hosting
  • Testing your JavaScript code

Here we are going to show you how to run a web server on your mac using Node JS.

If you haven't installed Node JS on your computer yet, click the link below.

Node js - www.nodejs.org

I suggest you download the recommended version. Wait for it to download... and open the package. The installation is pretty straight forward just click continue until its installed.

When the installation is finished, go to search, and open the terminal. There is a great module for node js, called http-server. We are going to use this module to run a web server straight from our computer.

  • To install this module we will use the node package manager, or in short npm. Because we need administrator rights we start with the sudo command, then we write "npm install http-server". And we use the -g command, which means global, this way we can use the web server in any folder on our compute


  • Press enter, input your computer account password and press enter again.

    Start our mentor-led course, where our mentor leads you on every step of the way. Claim your FREE trial today.

    Free trial


  • When the installation is finished, we have to move to the folder where we have the web page we want to run. Write ls -l, this is a command that lists all the files in the directory, so you can see where you are and where you have to go.


  • In my case I have the web page in a folder on my desktop, so I first write "cd desktop".


  • Then, using the "cd" command go to the exact location of you index.html file.
  • Now that we are in the right folder, we run the web server.
  • We write "http-server", and we add a "." which indicates that the server should run in this folder.


  • Press enter.... Copy the address and the port number - http://127.0.0.1:8080


  • Open your browser and paste the address with the port number.

 

There you have it, you are successfully running a web server on your mac.

I advise you to stop the server when you are done testing. To stop the server just go back to the terminal and click the CTRL+C key simultaneously.