github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/docs/03-getting-started.md (about)

     1  ---
     2  title: Getting Started
     3  ---
     4  
     5  The simplest Up application is a single file for the application itself, with zero dependencies, and an `up.json` file which requires only a `name`.
     6  
     7  If the directory does not contain an `up.json` file, the first execution of `up` will prompt you to create it, or you can manually create an `up.json` with some preferences:
     8  
     9  ```json
    10  {
    11    "name": "appname-api",
    12    "profile": "companyname",
    13    "regions": ["us-west-2"]
    14  }
    15  ```
    16  
    17  Up runs "vanilla" HTTP servers listening on the `PORT` environment variable, which is passed to your program by Up. For example create a new directory with the following `app.js` file:
    18  
    19  ```js
    20  const http = require('http')
    21  const { PORT = 3000 } = process.env
    22  
    23  http.createServer((req, res) => {
    24    res.end('Hello World from Node.js\n')
    25  }).listen(PORT)
    26  ```
    27  
    28  Deploy it to the staging environment:
    29  
    30  ```
    31  $ up
    32  ```
    33  
    34  Open up the URL in your browser:
    35  
    36  ```
    37  $ up url --open
    38  ```
    39  
    40  Or test with curl:
    41  
    42  ```
    43  $ curl `up url`
    44  ```
    45  
    46  That's it! You've deployed a basic Up application. To view further help for commands use:
    47  
    48  ```
    49  $ up help
    50  $ up help COMMAND
    51  $ up help COMMAND SUBCOMMAND
    52  ```
    53  
    54  If you're not a Node.js developer here are some examples in additional languages.
    55  
    56  For Python create `app.py`:
    57  
    58  ```python
    59  from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
    60  import os
    61  
    62  class myHandler(BaseHTTPRequestHandler):
    63    def do_GET(self):
    64        self.send_response(200)
    65        self.send_header('Content-type','text/html')
    66        self.end_headers()
    67        self.wfile.write("Hello World from Python\n")
    68        return
    69  
    70  server = HTTPServer(('', int(os.environ['PORT'])), myHandler)
    71  server.serve_forever()
    72  ```
    73  
    74  For Golang create `main.go`:
    75  
    76  ```go
    77  package main
    78  
    79  import (
    80    "os"
    81    "fmt"
    82    "log"
    83    "net/http"
    84  )
    85  
    86  func main() {
    87    addr := ":"+os.Getenv("PORT")
    88    http.HandleFunc("/", hello)
    89    log.Fatal(http.ListenAndServe(addr, nil))
    90  }
    91  
    92  func hello(w http.ResponseWriter, r *http.Request) {
    93    fmt.Fprintln(w, "Hello World from Go")
    94  }
    95  ```
    96  
    97  Finally for Crystal create `main.cr`:
    98  
    99  ```ruby
   100  require "http/server"
   101  
   102  port = ENV["PORT"].to_i
   103  
   104  server = HTTP::Server.new(port) do |ctx|
   105    ctx.response.content_type = "text/plain"
   106    ctx.response.print "Hello world from Crystal"
   107  end
   108  
   109  server.listen
   110  ```