This project is for demonstrating how to implement file upload functionality using the NodeJS framework LoopBack, creating a reference on how to recreate it step by step.
slc loopback
npm install loopback-component-storage --save
slc loopback:datasource
container
loopback-component-storage
server/datasources.json
and is now available in the explorer as a new datasourceConfigure the container datasource with the desired provider and options. For this example we will be using the filesystem provider, but others (Amazon, Rackspace, Azure, etc) are available and documented here
uploads
In server/datasources.json
locate the container object and add the provider
& root
attributes, setting the provider to filesystem
, and the root to your desired filesystem location. For this example we will be setting root to ./uploads
.
"container": {
"name": "container",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./uploads"
}
Container
model / REST API endpoint & configure it to utilize the new container datasourceslc loopback:model
Container
container (loopback-component-storage)
from the listPersistedModel
from the listYes
Now that our endpoint is configured and ready to be consumed you can fire up the app and test it out. For this example we'll demonstrate how to create a new container folder and upload a file to that container. However, in addition to the common LoopBack auto generated endpoints you'll find that the loopback-component-storage module exposes a few new endpoints that are documented here.
slc run .
Using your REST client, POST to http://localhost:3000/api/Containers/
, passing a data object with a name attribute (which is the name for the new container)
{
"name": "images"
}
./uploads/images/
Create another POST, this time to http://localhost:3000/api/Containers/images/upload
, passing a file
attribute which is your file to upload.
{
"file": [fileobject]
}
./uploads/images
and you will see your uploaded file.git clone git@github.com:justinwoodcock/Loopback-Upload.git
npm install
slc run .