【发布时间】:2018-02-11 23:06:33
【问题描述】:
我有一个平均堆栈应用程序。在后台,我有api.js:
var express = require('express')
var router = express.Router();
var body = 'response.send("hello fixed")';
var F = new Function ("request", "response", body);
router.get('/api/special', F);
module.exports = router;
因此,https://localhost:3000/api/special 在浏览器中返回 hello fixed。
现在,我想让前端定义body的内容。例如,我可以在网页中创建一个文本区域,用户可以随意输入例如response.send("hello flexible")。然后,我需要将此内容传递给 nodejs,以便 https://localhost:3000/api/special 现在返回 hello flexible。
有谁知道如何做到这一点?
编辑1:根据李安的评论,我修改了api.js:
var express = require('express')
var router = express.Router();
router.put('/endpoint', function (req, res, next) {
console.log("api.js router.put /endpoint");
router.get('/api/special', eval('(' + req.body.value + ')'))
})
module.exports = router;
在控制器中:
app.controller('EndpointCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.body = 'function (request, response) { response.send("Hello") }';
$scope.changeBody = function (body) {
return $http.put('/endpoint', { value: body })
}
}])
奇怪的是,它只在我第一次put /endpoint 时起作用,而后来put 似乎没有更新/api/special。有谁知道为什么?
另外,在后端的控制台中看到第二个或第三个put /endpoint需要几秒钟,这是有线的。
【问题讨论】:
-
你为什么希望前端的人这样修改你的后端?巨大的传入安全漏洞。
-
有时,制作像here这样灵活的api很有用。
-
但是 RunKit 做了一些过滤和清理。您也必须实现这一点,以防止人们将您的应用用作攻击媒介。
-
嗯,我会的……但首先,我需要知道他们是如何做到这一点的……
-
你可以设置一个端点来接收像
/api/create_endpoint/这样的函数体,然后POST到那个URL来创建一个带有输入值的新端点。
标签: javascript node.js api express routes