{"id":19020250,"url":"https://github.com/trailsjs/trails-file-upload-example","last_synced_at":"2026-04-28T07:30:21.068Z","repository":{"id":69884634,"uuid":"67229247","full_name":"trailsjs/trails-file-upload-example","owner":"trailsjs","description":"Describes how to upload files using Trails.","archived":false,"fork":false,"pushed_at":"2016-09-08T19:24:19.000Z","size":45,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-02T00:25:40.382Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trailsjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-02T14:26:45.000Z","updated_at":"2016-12-14T07:48:21.000Z","dependencies_parsed_at":"2023-02-28T18:46:19.983Z","dependency_job_id":null,"html_url":"https://github.com/trailsjs/trails-file-upload-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrails-file-upload-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrails-file-upload-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrails-file-upload-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrails-file-upload-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trailsjs","download_url":"https://codeload.github.com/trailsjs/trails-file-upload-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240064608,"owners_count":19742347,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T20:16:20.264Z","updated_at":"2026-04-28T07:30:19.287Z","avatar_url":"https://github.com/trailsjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trails File Upload Example\n\nThis repo describes how to upload files using the `multer` middleware with `trailpack-express`.\n\nTrailsMail is an imaginary email service that allows you to send an email with an attachment to an email address of your choice.  Once the message and attachment have been received by the server, different statistics about the message are displayed.\n\n## Setup\n\n```js\n  git clone git@github.com:trailsjs/trails-file-upload-example.git \u0026\u0026 cd trails-file-upload-example\n  npm install\n  npm start\n```\n\nOpen `localhost:3000` in your browser of choice.\n\n## Flow\n\nUser fills out a form with fields `recipient`, `subject`, `message`, and `file`.  \n```html\n\n\u003c!-- views/index.html --\u003e\n\n\u003cform class=\"pure-form pure-form-stacked\" action=\"/message\" method=\"post\" enctype=\"multipart/form-data\"\u003e\n  \u003cfieldset\u003e\n    \u003clabel for=\"recipient\"\u003eRecipient\u003c/label\u003e\n    \u003cinput name=\"recipient\" id=\"recipient\" type=\"email\" placeholder=\"Email\"\u003e\n\n    \u003clabel for=\"subject\"\u003eSubject Line\u003c/label\u003e\n    \u003cinput name=\"subject\" id=\"subject\" type=\"text\" placeholder=\"Subject Line\"\u003e\n\n    \u003clabel for=\"message\"\u003eMessage\u003c/label\u003e\n    \u003ctextarea name=\"message\" rows=\"5\" cols=\"40\" placeholder=\"Your message here\"\u003e\u003c/textarea\u003e\n\n    \u003clabel for=\"subject\"\u003eAttachment\u003clabel\u003e\n    \u003cinput name=\"file\" type=\"file\" placeholder=\"Password\"\u003e\n    \u003cbutton class=\"pure-button pure-button-primary\"\u003eSend\u003c/button\u003e\n  \u003c/fieldset\u003e\n\u003c/form\u003e\n```\n\nOn submit, the browser makes a request to `POST /message`, which is directed towards `MessageController#send`.\n\n```js\n// config/routes.js\nmodule.exports = [\n...\n  {\n    method: [ 'POST' ],\n    path: '/message',\n    handler: 'MessageController.send'\n  }\n...\n]\n```\n\nIn `config/policies.js`, note that a Policy has been configured for `MessageController#send`, so the request will be handled by `MessagePolicy#single` on it's way to `MessageController#send`\n```js\n// config/policies.js\nmodule.exports = {\n  ...\n  MessageController: {\n    send: [ 'MessagePolicy.single' ]\n  }\n  ...\n}\n```\n\nIn `MessagePolicy`, we can use Express middleware to modify the request.\nIn this example, files are saved to an uploads directory within the project\ndirectory.\n\n```js\n  // api/policies/MessagePolicy.js\n\n  ...\n\n  const multer = require('multer')\n  const upload = multer({dest: 'uploads/'})\n\n  ...\n\n  module.exports = class MessagePolicy extends Policy {\n    single (req, res, next) {\n      upload.single('file')(req, res, err =\u003e {\n        if (err) {\n          this.log.info(err)\n        }\n        next()\n      })\n    }\n  }\n\n```\n\nOnce the request object has arrived at `MessageController#send` `body` and `file` properties have been added by `multer.`\n\n```js\n  // api/controllers/MessageController.js\nmodule.exports = class MessageController extends Controller {\n  ...\n    send (req, res) {\n\n    this.log.info('Form Body')\n    this.log.info(req.body)\n\n    this.log.info('Attachment')\n    this.log.info(req.file)\n\n    res.render('sent', {\n      recipient: req.body.recipient,\n      subject: req.body.subject,\n      message: req.body.message,\n      file: req.file\n    })\n\n  }\n  ...\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailsjs%2Ftrails-file-upload-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrailsjs%2Ftrails-file-upload-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailsjs%2Ftrails-file-upload-example/lists"}