Friday, April 29th 2016 (over 1 year ago)

Serve It, now

Leo Lamprecht (@notquiteleo)

A particularly exciting capability of now is that it enables the seamless global delivery of JavaScript applications.

It's a general solution for serving both dynamic and static applications in the cloud.

Today I want to share a wrapper of now I've written called now-serve or ns for short.

With just one command, you can instantly upload and serve:

  • Static sites
  • Entire directories listings
  • … and even single files!

This is how you get it:

▲ npm install -g now now-serve

and here are some examples of it in action.

Static site hosting

▲ ls my-project
index.html     logo.png     images/

▲ ns my-project

On the left, your static site. On the right, adding /_src shows the code.

Sharing assets

▲ ls my-logos
white.png     white@2x.png     black.png
  black@2x.png
▲ ns my-logos
Example of a directory listing

Single file

▲ ns logo.png
Example of a single file upload

Under the hood

If you look at the source code (by adding /_src to a link) of any of those now deployments, you'll see that allns does is simply create a package.json for you and upload the files.

{
  "name": "ns-{random}",
  "dependencies": {
    "list": "latest"
  },
  "scripts": {
    "start": "list"
  }
}

The command list is supplied by the module list. It's also possible to use http-server or serve or any custom npm module for serving files by simply supplying the desired module and command combination to ns:

▲ ns -c 'http-server -d' -p http-server

Since all we do is use now under the hood in a programmatic way, you get all its great capabilities like fast uploads and file de-duping.

And, of course, every single time you share a new fresh link is provided for you.

The source, please

All the modules used for this utility have been released under the MIT license.
Check them out:

All contributions and feedback are welcome!