Visitar URL original
hapi-learning/11-UPLOADS at master · nodejs-spain/hapi-learning · GitHub
Skip to content

Latest commit

 

History

History

11-UPLOADS

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

readme.md

UPLOADS

Exercise 11 of 13

Create a server with an endpoint that accepts an uploaded file to the following path:

    /upload

The endpoint should accept the following keys: description and file. The description field should be a string describing whatever you want, and file should be an uploaded file. The endpoint should return a JSON object that follows the following pattern:

    {
      description :  //description from form
      file : {
        data :    //content of file uploaded
        filename:  //name of file uploaded
        headers :   //file header provided by hapi
      }
    }

HINTS

To accept a file as input, your request should use the multipart/form-data header.

We can get a file as readable stream by adding the following in the route configuration:

    
    payload: {
        output : 'stream',
        parse : true
    }

If we've uploaded the file with the parameter file, then we can access it in the handler function using following code:

    handler: function (request, reply) {
        var body = '';
        request.payload.file.on('data', function (data){
    
          body += data
        });
    
        request.payload.file.on('end', function (){
    
          console.log(body);
        });
    }

More information about file uploading can be found in the reply interface of the hapi API docs.

  • To print these instructions again, run: makemehapi print
  • To execute your program in a test environment, run: makemehapi run program.js
  • To verify your program, run: makemehapi verify program.js
  • For help run: makemehapi help