无法获得签名网址而不是黑洞

时间:2019-05-27 18:56:16

标签: node.js api express pre-signed-url

我正在使用下面的已签名包

https://www.npmjs.com/package/signed

,然后将已签名const的结果导入到与声明它的文件不同的文件中。当我尝试在已签名的网址上使用signed.verifier()方法时,每次都会被“黑洞”

这是相关src / models / pictures.js的代码

    import signed from 'signed';
    import User from './users'

    export const signature = signed({
        secret: 'secret string'
      });
const pictureSchema = new mongoose.Schema({
    title: {type: String, maxlength: [50, 'Title must be longer than 50 characters']},
    description: {type: String},
    filename: {type: String},
    thumbname:{type: String},
    thumbswitch:{type: Boolean, default: false},
    user: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
});

pictureSchema.statics.activePictureUrl = function(data) {
    return data.map(function(picture) {
        return{
            _id: picture._id,
            title: picture.title,
            description: picture.description,

// here's the signatre of the urls passed to the user.

            filename:signature.sign(
                'http://localhost:8080/api/pictures/file/?filename=' + picture.filename),
            thumbname:signature.sign(
                'http://localhost:8080/api/pictures/file/?filename=' + picture.thumbname),
            user: picture.user
        }
    })
}

var picture = mongoose.model('Picture', pictureSchema);

exports.picture = picture;

它返回如下值:

{
    "message": [
        {
            "_id": "5ceb6f4e0fc95861c3d88810",
            "title": "botty",
            "description": "lorem ipsum blah",
            "filename": "http://localhost:8080/api/pictures/file/?filename=image-1558933326636.jpg&signed=r:4808004935;e:1558984163;cea1a69130e6d0e23279a6f12a9bd557",
            "thumbname": "http://localhost:8080/api/pictures/file/?filename=thumb-image-1558933326636.jpg&signed=r:9199843540;e:1558984163;a9f621b1399b070a39f758789bf755e8",
            "user": "5cdca3a16c095a53c8b159c2"
        },

这是我在src / api / pictures.js文件中所做的导入:

import multer from 'multer';
import path from 'path';
import { Router } from 'express'
import { picture } from '../models/pictures'
import { verifyJWT_MW } from '../middleware/auth'
import { signature } from '../models/pictures'
var gm = require('gm').subClass({imageMagick: true});

const pics = Router()

// Here's the relevant code:

pics.get('/file/',signature.verifier(), (req, res, next) => {
  let filename = req.query.filename;
  res.sendFile(path.resolve('./storage/media') + '/' + filename);
})

将返回如下所示的错误:

Error: Blackholed
    at blackholed (/home/emi/projects/nodetest/node_modules/signed/src/index.ts:99:25)
    at /home/emi/projects/nodetest/node_modules/signed/src/index.ts:116:28
    at Layer.handle [as handle_request] (/home/emi/projects/nodetest/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/emi/projects/nodetest/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/emi/projects/nodetest/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/emi/projects/nodetest/node_modules/express/lib/router/layer.js:95:5)
    at /home/emi/projects/nodetest/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:335:12)
    at next (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:174:3)
    at router (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/emi/projects/nodetest/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:317:13)
    at /home/emi/projects/nodetest/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:335:12)
    at next (/home/emi/projects/nodetest/node_modules/express/lib/router/index.js:275:10)

问题是,当我下载实际的模块存储库并运行测试时,测试返回的结果与预期的完全相同,而我似乎无法使该软件包对我有用。

任何帮助将不胜感激。预先谢谢你:)

0 个答案:

没有答案