小编Sim*_*mer的帖子

如何在Sails.js中为自定义路线启用CORS

我有一个Angular 1.x应用程序,该应用程序在Sails.js应用程序中调用API。每当我尝试从Angular应用中调用API时,都会得到-

XMLHttpRequest cannot load http://localhost:1337/portal/login. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'http://localhost:8080' is therefore not allowed access.

由于我的Sails.js应用程序还有很多其他API不会在此Angular应用程序上使用,因此我不想通过allRoutes: true在config / cors.js中进行设置将CORS应用于所有这些API。因此,我遵循了Sails.js的文档,并以这种方式编写了自定义CORS配置-

    '/portal/login': {
        target: 'MyController.login',
        cors: {
            origin: '*',
            credentials: true,
            methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH',
            headers: 'content-type, Authorization'
        }
    }
Run Code Online (Sandbox Code Playgroud)

但它不起作用。如果启用,allRoutes: true则它开始工作,但我不想在所有路由上启用CORS并公开它们。我已经尝试过所有可能的组合,origin, credentials, methods, headers但始终会给出相同的错误。

您能帮我解决这个问题吗?提前谢谢。

javascript node.js cors angularjs sails.js

6
推荐指数
1
解决办法
510
查看次数

如何使用动态值将“name”属性添加到 <mat-cell> 或 <mat-row> ?

我有一次mat-table渲染大约 10 行。该表的数据源来自作为对象数组的 API 调用。我想向其中的元素添加一个name属性,或者向其值作为与该行的索引连接的字符串的元素添加一个属性。mat-cellmat-row

我能够分别通过*matCellDef="let i = index"和获取索引*matRowDef="let i = index;"。但是,如果我添加name具有值的属性,name="monitoring{{i}}"那么它不会将该属性添加到该标签/元素。{{i}}但是,如果我从值中删除(这使得它name="monitoring"),那么它将被分配该字符串并且将完美显示,但每行的名称变得相同。

有谁知道如何将动态值分配给 name 属性,以及我在分配中做错了什么,以致它不会出现在动态值分配中?

提前致谢。

PS 这是我正在尝试使用的代码 -

不起作用:(

<mat-row *matRowDef="let row; columns: displayedColumns; let i = index;" name="monitoring{{i}}"></mat-row>

作品

<mat-row *matRowDef="let row; columns: displayedColumns;" name="monitoring"></mat-row>

typescript angular-material angular-material2 angular

3
推荐指数
1
解决办法
2837
查看次数

Facebook messenger API请求正文中的内容不完整

解释有点长,请耐心等待.

我正在构建一个Facebook messenger bot,它在后端和MongoDB数据库中使用我的sails.js/node.js服务器.

在我的sails应用程序中,我已经将策略应用于控制器的方法,该方法处理在从用户接收文本之后要执行的操作.在本政策中,我遵循文档(https://developers.facebook.com/docs/messenger-platform/webhook-reference - "安全性"部分)并将x-hub-signature请求标题中的内容与sha1摘要进行比较.请求有效载荷(正文).

因此,现在每当我向机器人发送消息时,它在策略中都表示请求中的签名和我计算的签名是不同的,因此不会更进一步.我仔细检查了应该在计算摘要时应该使用的app秘密,它似乎是正确的.我发现的另一个不同之处在于,Facebook请求还在其标题中发送"内容长度"字段,这与他们在同一请求中发送的正文的字符长度不同.这就是我认为不同签名的原因,但我无法解决它并找到问题的根源,为什么会发生这种情况.

另外需要注意的是,抛出此不匹配错误的相同代码在某些时间(实际上,大多数时间)运行完美.

有人可以帮帮我吗?我将永远感激:)

这是政策中的代码

var crypto = require('crypto');
if(req.headers['x-hub-signature']){
    //console.log('req headers -----', JSON.stringify(req.headers));
    //console.log('req body -----', JSON.stringify(req.body));

    var hmac, calculatedSignature, payload = req.body;
    hmac = crypto.createHmac('sha1', app_secret);
    hmac.update(JSON.stringify(payload));
    calculatedSignature = 'sha1='+hmac.digest('hex');

    //console.log("signature calculatedSignature",calculatedSignature);
    if(calculatedSignature === req.headers['x-hub-signature']){
        return next();
    }else{
        res.forbidden('You shall not pass!');
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个示例请求标头 -

{"host":"e93d4245id.ngrok.io","accept":"*/*","accept-encoding":"deflate, gzip","content-type":"application/json","x-hub-signature":"sha1=d0cd8177add9b1ff367d411942603b0d08183964","content-length":"274","x-forwarded-proto":"https","x-forwarded-for":"127.0.0.1"}
Run Code Online (Sandbox Code Playgroud)

而这是来自同一要求的身体 -

{"object":"page","entry":[{"id":"1778585282425767","time":1479476014038,"messaging":[{"sender":{"id":"userId"},"recipient":{"id":"recipientId"},"timestamp":1479468097895,"message":{"mid":"mid.1479468097895:efdc7d2c68","seq":2355,"text":"Hahahaha"}}]}]}
Run Code Online (Sandbox Code Playgroud)

javascript node.js messenger sails.js facebook-messenger

2
推荐指数
1
解决办法
304
查看次数