Image upload with node storing on a seperate directory
why do anything so fancy as S3
- tags
- flyio
- vite
- javascript
Contents
|
|
This will give us something like
package.json
:
|
|
Server App
Our first bit of code is going to deal with moving the files around.
express-fileupload
will get the file and put it in a temp directory,
and this will handle moving the code into our data directory. If
DATA_DIR
is in the enviroment it will put it there, otherwise it will
stick it in a local uploads/
folder.
file_storage.js
:
|
|
Now to the app itself. We'll serve our javascript application out of
dist/
which is where vite will eventually build it, create an /images
GET route to return a list of images that we have in our folder, an
/images/:name
route which will return the image, and an /upload
POST
route to accept the uploaded file and store it in the directory.
app.js
:
|
|
Client App
Here we are splitting up the code into two different components,
capture-photo
and photo-list
. Clearly some work could be done on
design.
index.html
:
|
|
This makes an input type=file
which will prompt the user for photo
(or, on mobile, go to the camera itself) and then post the file back
to the server. Either the express app running on port 3000 locally or
whatever the base url of the deployed site is in production.
After it's done it dispatches a refresh
event on the window with the
other component can watch for.
capture-photo.js
:
|
|
Hit the endpoint, and make the list of images!
photo-list.js
:
|
|
Deploy
Lets make sure that this junk doesn't end up in the docker image.
.dockerignore
:
|
|
Build the vite app, then run the express app.
Dockerfile
:
|
|
The key parts of this file are the [mounts]
section which defines the
persistent storage and the [env]
section which tells our code where it
is.
fly.toml
:
|
|
Previously
Next