request.on('data', function (chunk) { fullBody += chunk.toString(); });
server.post('/upload', function (req, res) { var fileLoc = path.join('./uploads', random.generate({ length: 10, charset:'alphabetic'}) + '.pdf'); var wstream = fs.createWriteStream(fileLoc); req.on('data', function(chunk) { console.log(chunk.length); wstream.write(chunk); // Not the best way to handle thsi call... }); req.on('end', function() { wstream.end(); // Not the best way to handle thsi call... res.status(200).send('Done'); }); });
// Custom middleware that keeps the raw request body. This is necessary to checksum // the data we receive in webhooks and ensure their authenticity. app.use((req, res, next) => { req.rawBody = ''; req.on('data', (chunk) => { req.rawBody += chunk; }); next(); });
function registerBaseRouter() { router.get('/base/get', function(req, res) { res.json(req.query) }) router.post('/base/post', function(req, res) { res.json(req.body) }) router.post('/base/buffer', function(req, res) { let msg = [] req.on('data', (chunk) => { if (chunk) { msg.push(chunk) } }) req.on('end', () => { let buf = Buffer.concat(msg) res.json(buf.toJSON()) }) }) }
//ffmpeg pushed stream in here to make a pipe app.all(['/streamIn/:ke/:id','/streamIn/:ke/:id/:feed'], function (req, res) { var checkOrigin = function(search){return req.headers.host.indexOf(search)>-1} if(checkOrigin('127.0.0.1')){ if(!req.params.feed){req.params.feed='1'} if(!s.group[req.params.ke].mon[req.params.id].streamIn[req.params.feed]){ s.group[req.params.ke].mon[req.params.id].streamIn[req.params.feed] = new events.EventEmitter().setMaxListeners(0) } //req.params.feed = Feed Number res.connection.setTimeout(0); req.on('data', function(buffer){ s.group[req.params.ke].mon[req.params.id].streamIn[req.params.feed].emit('data',buffer) }); req.on('end',function(){ // console.log('streamIn closed',req.params); }); }else{ res.end('Local connection is only allowed.') } })
app.post('/stream-json', (req, res) => { if (hasAuth(req)) { const id = req.body.id; let ref = setInterval(() => { const str = newJson(streamNext(id)); console.log(str); res.write(str); }, 10); const done = () => clearInterval(ref); req.on('close', done); req.on('end', done); setTimeout(() => { done(); res.end(']'); }, 10 * second); } else { res.json({ status: 'denied' }); } });
// req.text (http://stackoverflow.com/questions/12497358/handling-text-plain-in-express-3-via-connect/12497793#12497793) app.use(function(req, res, next){ if (req.is('text/*')) { req.text = ''; req.setEncoding('utf8'); req.on('data', function(chunk){ req.text += chunk; }); req.on('end', next); } else { next(); } });
server.put("/customers/30000001", (req, res) => { let body = []; req .on("data", chunk => { body.push(chunk); }) .on("end", () => { body = JSON.parse(Buffer.concat(body).toString()); console.log(JSON.stringify(body)); if (body.age && body.age < 18) { console.log("Validation Error"); return res.send({ error: true, validation: { age: "Must be 18 years or older", name: "Name is incorrect" } }); } else { res.send("ok"); } }); });
app.post('/square', (req, res) => { let number = ''; req.on('data', (data) => { number = number + data; }); req.once('end', () => { res.end(Math.pow(+number, 2) + ''); }); });
app.use((req, res, next) =>{ req.on('static', () => { var file = url.parse(req.url).pathname console.log(file); var mode = 'stylesheet' if (file[file.length - 1] === '/') { file += 'index.html' mode = 'reload' } createWatcher(file, mode) }) next() })
// Custom Middleware to compute rawBody. Unfortunately using // JSON.stringify(req.body) will remove spaces and newlines, so verification // will fail. We must add this middleware to ensure we're computing the correct // signature app.use(function(req, res, next) { req.rawBody = ''; req.on('data', (chunk) => (req.rawBody += chunk)); req.on('error', (err) => res.status(500).send('Error parsing body')); req.on('end', () => { // because the stream has been consumed, other parsers like bodyParser.json // cannot stream the request data and will time out so we must explicitly parse the body try { req.body = JSON.parse(req.rawBody); next(); } catch (err) { res.status(500).send('Error parsing body'); } }); });