This repository contains a JSON express server, which is both the backend AND the frontend for a Jasonette app that lets you upload photos to S3.
You can try the demo by playing https://imagejason.herokuapp.com on Jasonette. Learn more
Here's an overview of how this app works (both client side and server side).
(Note: Remember that on Jasonette, there's no code you need to write on the client side, you're basically building the client interface on the server. The JSON response is your app)
- When the app starts, it starts an express server to accept web requests.
- The server also connects to a mongodb instance
- When a Jasonette client makes a
GET
request to the root url (/
), the server responds with a JSON markup. - The Jasonette client will interpret the JSON and render a native app on its side, which allows the user to take a photo by swiping down.
- After the user takes a photo, it makes a
GET
request to/sign_url
to get an S3 signed url. - The server generates and returns a signed url.
- The Jasonette client uploads the photo directly to S3 using the signed url.
- After the upload is over, it makes a
POST
request to the express server's/post
endpoint, with the s3 image url. - The express server stores the url to the mongodb it's connected to.
- Then it returns a JSON which contains the list of all the images in the DB.
- The Jasonette client displays the result.
The response JSON markup describes how the view will be displayed, as well as actions.
There are a couple of important actions being used here.
- Takes a photo using $media.camera.
- Then it makes a request to our server's
/sign_url
endpoint using $network.request. - Then it uploads it to the S3 signed url using $network.upload.
- After the upload is over, it makes a
POST
request to our server's/post
endpoint with the S3 filename, and the server stores it, so it can be displayed.
- The server initializes a Mongo DB instance via
init.DB()
, and then starts an express web server viainit.server()
. - When a Jasonette client hits the root URL (
/
), the server returns the JASON Markup, and the client renders it accordingly. - The Jasonette client listens for a
$pull
event, and when it happens (User pulls to refresh), it makes a POST request to/sign_url
endpoint. - Upon a
/sign_url
POST request, the server generates and returns an S3 signed url using theaws
module. - Jasonette client then takes the url and uploads the photo to that URL immediately.
- Once the upload has finished, the Jasonette client makes a
$network.request
to/post
endpoint. - The backend responds by storing that entry to the DB.
You need to set the following environment variables to make the code work on your own server.
- MONGODB_URI - Set up a mongodb instance and use its URL (In my case I used a free instance of mlab)
- S3_KEY - Your S3 key
- S3_SECRET - Your S3 secret