Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinrodbe/55ecbc4e96824dd1d9fddc05f56cb8a5 to your computer and use it in GitHub Desktop.
Save kevinrodbe/55ecbc4e96824dd1d9fddc05f56cb8a5 to your computer and use it in GitHub Desktop.

Revisions

  1. @rauchg rauchg revised this gist Apr 13, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion zero-to-microservice-with-now.md
    Original file line number Diff line number Diff line change
    @@ -158,4 +158,6 @@ To deploy it, I simply type `now` again and I'll get a new URL.

    And voila! I can now send parameters. Both my URLs still work. `now` never overwrites or throws away your work.

    ![](https://cloudup.com/cFatYEe9AqS+)
    ![](https://cloudup.com/cFatYEe9AqS+)

    If you have any questions about this tutorial, please comment below!
  2. @rauchg rauchg revised this gist Apr 13, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion zero-to-microservice-with-now.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    ## From zero to microservice with 𝚫 now

    The following guide will show you how to deploy a simple microservice written in JavaScript using [𝚫 now](https://zeit.co/now).
    It uses tools that are widely available, tested and understood:

    It uses Open Source tools that are widely available, tested and understood:

    - Node.JS
    - NPM
  3. @rauchg rauchg revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zero-to-microservice-with-now.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ## From zero to microservice with 𝚫 now

    The following guide will show you how to deploy a simple microservice written in JavaScript.
    The following guide will show you how to deploy a simple microservice written in JavaScript using [𝚫 now](https://zeit.co/now).
    It uses tools that are widely available, tested and understood:

    - Node.JS
  4. @rauchg rauchg renamed this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## From zero to microservice
    ## From zero to microservice with 𝚫 now

    The following guide will show you how to deploy a simple microservice written in JavaScript.
    It uses tools that are widely available, tested and understood:
  5. @rauchg rauchg revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zero-to-microservice.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ To make sure Node.JS (which comes with `npm`) is properly installed, try running
    npm --version
    ```

    It should look like this:
    It should look similar to this:

    ![](https://cloudup.com/cebs58aBhVA+)

  6. @rauchg rauchg revised this gist Apr 13, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions zero-to-microservice.md
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,10 @@ npm install -g now
    To verify it's installed and it works run:

    ```
    now --help
    now --version
    ```

    It should look like this:
    It should look similar to this:

    ![](https://cloudup.com/c4ZUmYsu8eg+)

  7. @rauchg rauchg revised this gist Apr 13, 2016. No changes.
  8. @rauchg rauchg created this gist Mar 13, 2016.
    160 changes: 160 additions & 0 deletions zero-to-microservice.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,160 @@
    ## From zero to microservice

    The following guide will show you how to deploy a simple microservice written in JavaScript.
    It uses tools that are widely available, tested and understood:

    - Node.JS
    - NPM
    - Express

    ### Prerequisites

    Make sure Node.JS is installed in your machine. If not, head to https://nodejs.org/en/ and download it and install it.

    To execute the commands described in this guide, launch your preferred Terminal. On Mac OS X, you can locate "Terminal.app" under "Applications" > "Utilities".

    To make sure Node.JS (which comes with `npm`) is properly installed, try running the command:

    ```
    npm --version
    ```

    It should look like this:

    ![](https://cloudup.com/cebs58aBhVA+)

    #### Installing now

    `now` is a tool that will immediately deploy any Node.JS HTTP service with all its files and dependencies to the cloud.

    Install it with NPM as follows:

    ```
    npm install -g now
    ```

    To verify it's installed and it works run:

    ```
    now --help
    ```

    It should look like this:

    ![](https://cloudup.com/c4ZUmYsu8eg+)

    ### Creating your service

    Create a fresh directory for it and go to it:

    ```bash
    mkdir ~/my-service
    cd ~/my-service
    ```

    Every Node.JS project needs a `package.json` file. Create it with some basic information by copying and pasting this entire command:

    ```
    cat <<EOF > package.json
    {
    "name": "my-service",
    "version": "0.1.0",
    "dependencies": {}
    }
    EOF
    ```

    To introduce a dependency, let's install express and save it to `package.json`:

    ```
    npm install [email protected] --save
    npm install [email protected] --save
    ```

    If you then look at the contents of `package.json`, it will look as follows:

    ![](https://cloudup.com/c6bN2x_Jg5U+)

    ### Writing your service

    We'll first write a basic HTTP endpoint that accepts POST requests.

    ```bash
    cat <<EOF > index.js
    var app = require('express')();
    var srv = require('http').createServer(app);
    var bodyParser = require('body-parser');
    app.post('/', function (req, res, next) {
    res.send('You POSTed to the micro-service!');
    });
    srv.listen(3000, function () {
    console.log('Listening on 3000');
    });
    EOF
    ```

    If you run

    ```bash
    node index
    ```

    you should see that it's listening on port `3000`:

    ![](https://cloudup.com/cwpL9YhyyxX+)

    In another instance of the terminal, try POSTing some data with the `curl` utility (you can also [learn more](https://gist.github.com/caspyin/2288960) about curl).

    ![](https://cloudup.com/ca0oi9Mo864+)

    This service is ready to be deployed! Let's say that instead of `localhost`, which only works on your computer, you wanted to share your micro-service with co-workers or the world.

    Simply run `now`! The first time you run it, it'll prompt you to log in or register. Just enter your email and watch your inbox.

    After you click the link it emails you, notice that it will try to deploy:

    ![](https://cloudup.com/cCT5RT5yC8j+)

    `now` has only one simple requirement: a `start` script must be defined in `package.json`. We can add it as follows:

    ```bash
    cat <<EOF > package.json
    {
    "name": "my-service",
    "version": "0.1.0",
    "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4"
    },
    "scripts": {
    "start": "node index"
    }
    }
    EOF
    ```

    In this case, the `start` script will run the command we manually ran earlier, to make the HTTP service start.

    Notice that `now` doesn't need to know about ports. Any port will work (including ephemeral ones!) as long as your service allows HTTP traffic.

    ![](https://cloudup.com/cD69_TiT_5z+)

    When you enter `now`, it will give you a unique URL for that deployment. Normally, you get this URL even before the deployment is complete.

    If you open it in a browser, you can see the progress of the _build_: the upload, the installation of dependencies and `npm start`.

    In this case, the deployment happens in under a second!

    ### Making changes

    Let's say you want to make some changes to the code you just wrote. In this example, I made it so a `name` field is expected as JSON input.

    ![](https://cloudup.com/caKMo02igl0+)

    To deploy it, I simply type `now` again and I'll get a new URL.

    And voila! I can now send parameters. Both my URLs still work. `now` never overwrites or throws away your work.

    ![](https://cloudup.com/cFatYEe9AqS+)