Slide 16
Slide 16 text
Julie Ng | DIY Full Stack JavaScript CI/CD | EnterJS | Slides Version 26 June 2018
Node.js in the Backend
Node.js in the Backend
WORKER EXAMPLE
WORKER EXAMPLE
Actual app.js only has 50 lines.
Queue is wrapper around busboy streaming-processor library.
import express from 'express';
queue.on('connect', () => {
// connected, but no routes
http.listen(port, () => { console.log(`=== [${workerName}] listening on ${port}… ===` ); });
});
queue.on('message', (attrs) => {
let route = `/api/upload/${attrs.id}`;
queue.transmit('ready', Object.assign({ route: route }, attrs));
// dynamic routes per request
http.post(`/upload/${attrs.id}`, (req, res) => {
let upload = new Upload(attrs.id, req.headers);
queue.transmit('begin', attrs);
upload.on('file:end', () => {
queue.transmit('done', attrs);
res.writeHead(200, { 'Connection': 'close' });
res.end("That's all folks!");
});
return req.pipe(upload);
});
});