Serve a Directory with Node.js

By  on  

As I mentioned in Serve a Directory with Python, sometimes you need a directory to be "served" instead of loading the file:// location within your browser.  In the past I would mess around with MAMPStack and swapping out httpdocs directories, but there are better tools these days.

If you prefer to use Node.js instead of Python, you can use the http-server package:

# Install via shell
npm install http-server -g

# Serve a directory
http-server

# Serve a directory at a specified port
http-server -p 2020

Check out the http-server NPM page to check out specific options, but I wanted to highlight how easy it can be to serve a directory with Node.js.  And for JavaScript lovers like me...Node.js is the way to go.

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!