Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Set the Server Port in Express Using Environment Variables


    In this lesson we learn how to use environment variables to set the port of an express.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: We'll change the port that this server is using. We'll use process.env for environment, and we'll create a new variable called port. If the port environment variable is not defined, we'll still use 8888 as a default.

    If you run this server, we see that the server is running on port 8888. If we do a next port and we set the port into our operating system to say 3000 and we run this server again, we can see that the server is now running on port 3000.

    If we open our browser, we go to localhost:8888, it can't be reached. If we go to 3000, we're getting a 404 in this case. If we go to 3000/status, we are getting the server time.

    That's how you set the port inside an environment variable.