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
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

Incredible Demos

  • By
    Animated AJAX Record Deletion Using MooTools

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript. The PHP - Content & Header The following snippet goes at the...

  • By
    Dynamic Waveform Visualizations with wavesurfer.js

    Waveform images are an awesome addition to boring audio widgets.  They can be functional as well as aesthetically pleasing, allowing users to navigate audio visually.  I recently found wavesurfer.js, an amazing waveform image utility that uses to Web Audio API to create super customizable...

Discussion

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