Tag: 请求

NodeJS请求中的callback

it('should respond to a GET request at /api/keywords/', function (done) { request.get( { 'url': 'http://localhost:8080/api/keywords/', 'json': true }, function (err, res, body) { expect(res.statusCode).toBe(200); expect(body.foo).toEqual('bar'); done(); }); }); 我正在用Jasminetesting框架在NodeJS上做一个应用程序,同时我也了解callback如何在asynchronous编程中工作的基础知识。 然而,在这个特定的代码片段中,我似乎无法绕过这个想法,即在为url和json传递值之后,request.get()函数中已经传递了一个函数。 我的问题是,像request.get(),我没有定义的函数如何接受callback函数作为参数? 在这个方法中有规定接受这样的参数,因为我没有find一个定义,它声明接受一个callback函数作为参数来执行。 我知道它可以被传递,但是它是如何被执行的,因为我没有写出request.get()的定义,所以我不能定义如何执行一个传递的函数或者甚至执行它。

GET请求上的NodeJS请求对象为空

我有一个代码,我试图从一个下拉菜单使用获取请求获得一些价值。 但是,当我点击提交,请求对象是空的。 我没有使用body parser和bodyParser.json并在路由之前声明它们,但是请求对象仍然是空的: var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var EventHubClient = require('azure-event-hubs').Client; const bodyParser = require('body-parser') app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.json()) console.log(''); }; app.get('/result', function(req, res){ res.sendFile(__dirname + '/result.html'); }); app.get('/index', function(req,res) { res.sendFile(__dirname + '/index.html'); }); app.get('/getJson', function (req, res) { console.log(req.body); }); http.listen(3000, function(){ console.log('listening […]

在Chrome中使用Nodejs进行双重请求

我正在做一个Nodejstesting来增加一个全局计数器,根据下面的脚本。 一切正常,除了Chrome刷新时执行两个连续的请求,导致计数器增加两倍(请参阅图像)。 在Postman和Firefox工作正常,只有一个请求发生刷新。 我已经完成了在Linux和Windows上运行应用程序的testing,并且Chrome中存在这个问题。 任何人有任何想法可能会发生什么? server.js const http = require('http'); const express = require('express'); const app = express(); const rotas = require("./route-test")(app); const port = 3000; const server = app.listen(process.env.PORT || port, function(){ console.log('App listening port: ', port); }); 路由test.js const myGlobalVars = require("./global-vars"); module.exports = (app) => { app.use('/', (req, res) => { res.end('Counter […]

使用请求从节点stream上传到S3的问题

使用Node.js ,我试图上传一个大的文件(700MB〜1GB),作为响应POST请求(使用请求 ),到我的S3桶。 使用aws-sdk for Javascript iv'e尝试了两种方法,但每个都有一个不同的问题: 方法 1 – 尝试在response事件上调用s3.upload()函数: const sendRequest = (data) => { try { return new Promise((resolve, reject) => { AWS.config.loadFromPath('./config/awsConfig.json'); let s3 = new AWS.S3({ params:{ Bucket:'myBucket', Key:'path/to/file.csv' } }); request({ method: 'POST', uri: 'https://www.example.com/apiEndpoint', headers: { host: 'example.com', 'content-type': 'application/json' }, body: JSON.stringify(data) }).on('response', (response) => { // […]

同时写入和读取节点stream

我试图下载一个video文件,然后立即开始stream式传输到客户端,我有他们都独立工作的stream,但我似乎无法得到我想要的function工作。 request.on('response', function (fileResponse) { fileResponse.pipe(fs.createWriteStream(`${__dirname}/../media/${channelName}/${media}.mp4`) } stream式传输现有video: const file = fs.createReadStream(`${__dirname}/../media/${channelName}/${media}.mp4`, {start, end}); const head = { 'Content-Range': `bytes ${start}-${end}/${fileSize}`, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4', }; res.writeHead(206, head); 如果我把第二段代码放到最初的一段代码的响应事件中(试图创buildstream然后开始同时阅读),它不起作用。 我正在试图做技术上可能的stream? 谢谢!

Node.js向Gmail API发送消息的POST请求

我正尝试使用服务器端node.js向Gmail Send Message API发送请求,但没有成功。 我收到以下错误: body: '{ "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "\'raw\' RFC822 payload message string or uploading message via /upload/ URL required" } ], "code": 400, "message": "\'raw\' RFC822 payload message string or uploading message via /upload/ URL required" } }' } oauth2token和raw的input参数是有效的,事实上,如果我使用Google OAuth 2操场( https://developers.google.com/oauthplayground )并使用令牌和raw作为电子邮件发送的值。 有人可以看到我错过了什么吗? […]

使用http.request获取数据的特定部分

我想要得到使用http.request得到的响应数据的特定部分。 我有一个50 MB的zip文件我想要asynchronous使用5请求获取数据,使下载更快的url。 我的计划是简单的要求1将得到0-10mb的数据,而请求2得到11-20mb等。 有没有办法在节点js中做到这一点。 任何帮助将不胜感激。 先谢谢了

NodeJS – path和请求模块| 如何重命名多个副本?

我目前使用的请求,文件系统和path基本上下载100多个URL的图像,并将其存储到本地目录。 我正在使用path来重命名本地目录中存在的任何文件。 我的代码看起来像这样 var download = function (url, dest, cb) { var file = fs.createWriteStream(dest, {flags: 'wx'}); var sendReq = request.get(url); //There's a bunch more code here but I removed it for the sake of relevance file.on('error', function (err) { // Handle errors if (err.code === "EEXIST") { //if image already exist, create new images […]

如何等待pipe道在form-data node.js模块中完成?

使用node.js中的表单数据模块,你可以做到这一点 form.pipe(request) 将表单数据发送到请求。 但是如何才能完成呢? 有没有像这样的callback form.pipe(request).done(function(err) {}); 有人知道吗? 谢谢

用超时解决一连串的承诺。 Promise.all

我有这个。 const getPrice = function(database){ return new Promise(function (resolve, reject){ var promises =[]; for(var i in database){ promises.push(Update.requestClassifieds(database[i])) } Promise.all(promises) .then(function(todos){ return resolve(todos); }) })} Update.prototype.requestClassifieds = function(item){ var ithis = this; return new Promise((resolve, reject) => { var input = {}; request({ url: '', method: "GET", json: true, body: input }, function (error, response, […]