# Hello

Hello everyone today I bring you a quick post on SailsJs Upload/Download files SailsJs is a really good nodejs framework, which allows you to quickly setup a RESTful API with a controller and a model (yes that easy), yet it allows you to do classic server-side rendering for websites including custom actions, views and whatever you could expect in a classic web development,Also! did I mention it has integrated Websocket support? via socket.io
It is a really powerful framework built on top of [express] if you haven’t checked it you should just go and try!

### Hands on!

This post assumes you know how to connect to a database (either mysql, postgre or mongo or just disk)
and that you know how to access a route from the url
Let’s make a model and a controller for our Files api/models/File.js

now let’s create our controller
api/controllers directory

We can either do it by hand or using sails command tools

and if you go to the /file/upload url you will see the next message:

If everything is working well up to this point, let’s add some logic there

if everything goes fine, you should be able to send files now and both the files are uploaded
and the database registers where are those files

now let’s do some downloads; In the same controller create a new action

navigate to /file/download and you should see that json message
searching it by id or by name, let’s see

sails is very flexible in the way it handles things so if you need to customize
either the uploaded file’s name you can pass it in the upload({}) method
as well as location.

### Bonus

in your config/routes.js file you can add custom routes and also change urls for existing ones
the string 'get /filedownloader/:id': 'FileController.download', tells sails to only accept GET requests to the /filedownloader/:id route, which is maped to the download action inside FileController.js file :id ends up being the id param which we will use to find our file registry.
The same goes for 'post /fileuploader': 'FileController.upload', which will only accept posts in the /fileuploader url