J
J
Jedi2018-12-16 04:12:40
Apache HTTP Server
Jedi, 2018-12-16 04:12:40

How to write it in Koa?

Good day. I wanted to rewrite the following code

spoiler
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/index.html'));
})

app.get('/audio', function(req, res) {
  const path = 'sample.mp3';
  const stat = fs.statSync(path);
  const fileSize = stat.size;
  const range = req.headers.range;

  if (range) {
    const parts = range.replace(/bytes=/, '').split('-');
    const start = parseInt(parts[0], 10);
    const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;

    const chunksize = (end-start) + 1;
    const file = fs.createReadStream(path, {start, end});
    const head = {
      'Content-Range': `bytes ${start}-${end}/${fileSize}`,
      'Accept-Ranges': 'bytes',
      'Content-Length': chunksize,
      'Content-Type': 'audio/mpeg',
    };

    res.writeHead(206, head);
    file.pipe(res);
  } else {
    const head = {
      'Content-Length': fileSize,
      'Content-Type': 'audio/mpeg',
    };
    res.writeHead(200, head);
    fs.createReadStream(path).pipe(res);
  }
});

app.listen(8000, function () {
  console.log('Listening on port 8000!');
});
from Express to Koa, but there were a lot of errors. The first error is most likely due to Response responses. The second error occurred with fs.createReadStream(path).pipe(res);. Please help solve the problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin, 2016-02-08
@armenka29

/index.php?a=welcome
is obtained as:
RewriteRule /welcome /index.php?a=welcome [L]
or
RewriteRule /(.*?) /index.php?a=$1 [L]
/index.php?a =profile&u=username
is obtained as:
RewriteRule /(.*?)/(.*?) /index.php?a=$1&u=$2
but just read about RewriteRule
, as I understand it, you just stupidly take the rules from somewhere and just don’t understand trying to stick in htaches ....

V
Vladlen Hellsite, 2018-12-16
@PHPjedi

Something like this.

const Koa = require('koa');
const Router = require('koa-router');

const fs = require('fs');
const { promisify } = require('util');

const fsStat = promisify(fs.stat).bind(fs);

const app = Koa();

const router = new Router();

router.get('/audio', async (context) => {
    const path = 'sample.mp3';
    const { size: filesize } = await fsStat(path);

    const range = context.get('range');

    context.type = 'audio/mpeg';

    if (!range) {
        context.length = filesize;
        context.body = fs.createReadStream(path);

        return;
    }

    const [startPart, endPart] = range.replace(/bytes=/, '')
        .split('-');

    const start = parseInt(startPart, 10);
    const end = endPart
        ? parseInt(endPart, 10)
        : filesize - 1;

    context.length = (end - start) + 1;
    context.status = 206;

    context.set({
        'Accept-Ranges': 'bytes',
        'Content-Range': `bytes ${start}-${end}/${filesize}`
    });

    context.body = fs.createReadStream(path, {
        start,
        end
    });
});

app
    .use(router.routes())
    .use(router.allowedMethods());

app.listen(8000, () => {
    console.log('Listening on port 8000!');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question