Created
January 5, 2018 16:43
-
-
Save fiveisprime/99605395e87f6b3d9af87f49316fe43b to your computer and use it in GitHub Desktop.
Upload solution for makemehapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
try { | |
const Hapi = require('hapi'); | |
const server = new Hapi.Server({ | |
host: 'localhost', | |
port: Number(process.argv[2] || 8080) | |
}); | |
server.route({ | |
method: 'POST', | |
path: '/upload', | |
config: { | |
handler: (request, h) => { | |
return new Promise((resolve, reject) => { | |
let body = ''; | |
request.payload.file.on('data', (data) => { | |
body += data; | |
}); | |
request.payload.file.on('end', () => { | |
let result = { | |
description: request.payload.description, | |
file: { | |
data: body, | |
filename: request.payload.file.hapi.filename, | |
headers: request.payload.file.hapi.headers | |
} | |
}; | |
return resolve(JSON.stringify(result)); | |
}); | |
request.payload.file.on('error', (err) => { | |
return reject(err); | |
}); | |
}); | |
}, | |
payload: { | |
output: 'stream', | |
parse: true, | |
allow: 'multipart/form-data' | |
} | |
} | |
}); | |
await server.start(); | |
} | |
catch (error) { | |
console.log(error) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment