Tag: http

混淆了节点顺序规则(同步/asynchronous)

我的例子是一个简单的HTTP服务器: http.createServer((req, res) => { if(req.method === `GET`){ if(req.headers.cookie === undefined){ let x = 1 let y = 2 let z = 30 } else{ let x = 10 let y = 20 let z = 3 } switch(req.url){ case `/`: // >>>> I need the appropriate variables here for the same client <<<< break […]

读取angular2中的静态文件为string

我有一个这样的angular2组件: @Component({ selector: 'demo', template: ` <div [innerHTML]="content"></div> `; }) export class demoComponent { @Input() step: string; private content: string = ''; ngOnInit () { if (this.step === "foo") { this.content = "bar"; } } } 我想用一些逻辑来决定最合适的模板来渲染。 我在myDomain.com/templates上有一系列由express提供的静态html文件。 所以我需要能够“读取” ./templates/xyz.html到我的ng2组件中的string。 与fs.ReadFileSync中的fs.ReadFileSync API类似。 我怎样才能做到这一点? HTTP调用? 文件阅读器?

Nodejs在一个函数中循环http请求

我已经能够使用bluebird在一个函数中并行运行一个http get请求数组,这个函数完成之后会返回每个请求的响应。 不过,我希望按顺序链接请求,而不会影响下面显示的当前函数中承诺的asynchronous行为。 var Promise = require('bluebird'); function buildCartWithItems(cartID,requests, cookie) { var promises = []; for (idx in requests) { var request = requests[idx]; // requests is an array containing the json records of each request parameter request["id"] = cartID; promises.push(new Promise(function(resolve, reject) { var options = { host: 'example.com', path: "/example/addToCart?"+param(request), headers: {'Cookie': cookie, […]

使用非http(coap)快速路由

我正在运行一个CoP服务器,而且由于我已经开始添加路由,维护自己的路由有点繁琐,所以我认为我会切换到express(我已经用于http服务器)。 现在,鉴于它使用基本上相同的格式server.on("request", (req, res) => {…}) ,我认为它是兼容的,但expression增加了不同的头不在Coap spec。 我想要做的只是使用我的应用程序的快速路由的便利。 这可能吗? 我目前正在这样做: const server = coap.createServer(…); const app = express(); server.on("request", app); 结果是这个错误: Uncaught Error: Unknown string to Buffer converter for option: X-Content-Type-Options at module.exports.toBinary (node_modules/coap/lib/option_converter.js:21:11) at OutMessage.setOption (node_modules/coap/lib/helpers.js:56:16) at Immediate.write (node_modules/finalhandler/index.js:164:9)

自签名证书错误与NodeJS请求

我在代理服务器中使用NodeJS的请求库。 我有它正常工作,但我想要更改代理,以便而不是转发HTTP请求到HTTP地址,入站HTTP请求转发到HTTPS地址。 但是当我运行并尝试使用代理时,出现以下错误: stream.js:74 throw er; // Unhandled stream error in pipe. ^ Error: self signed certificate in certificate chain 我遵循在线说明来创build一个密钥,crt和一个假证书颁发机构(链接到代码中),但上面的错误表明它仍然不能正常工作。 我做什么显然是错的? 我的代码如下所示: var certFile = path.resolve(__dirname, 'server.CA-signed.crt'); var keyFile = path.resolve(__dirname, 'server.CA.key'); var ca = path.resolve(__dirname, 'demoCA/newcerts/1234567890.pem'); app.use(function(req, res) { var rewriter = request({ url: rewrite(req.url), qs: req.query, cert: fs.readFileSync(certFile), key: fs.readFileSync(keyFile), passphrase: 'foobar', […]

节点静默地closuresURL中的文字空间的请求

让我们开始简单的服务器: var http = require('http'); http.createServer(function (req, res) { console.log('asdasd'); res.end('asdasd'); }).listen(8898) 并提出一个简单的要求 curl -v 'localhost:8898/?ab' * Trying ::1… * Connected to localhost (::1) port 8898 (#0) > GET /?ab HTTP/1.1 > Host: localhost:8898 > User-Agent: curl/7.43.0 > Accept: */* > < HTTP/1.1 200 OK < Date: Thu, 13 Oct 2016 20:26:14 GMT < Connection: […]

Node JS / Express:如何parsing外部API调用的多部分响应

我使用npm Request模块进行外部API调用,该模块返回多部分表单数据。 以下是请求的代码段(不完整): request.get(options, function (err, httpResponse, body) { if (err) { return console.error('Error:', err); } console.log(body); }); 以下是响应主体(上面logging的控制台)的示例: –multipartBoundary Content-Disposition: form-data; name="file"; filename="loremipsum.sample" Content-Type: application/octet-stream Content-Transfer-Encoding: base64 TG9yZW0gSXBzdW0gaXMgc2ltcGx5IGR1bW15IHRleHQgb2YgdGhlIHByaW50aW5nIGFuZCB0eXBlc2V0dGluZyBpbmR1c3RyeS4gTG9yZW0gSXBzdW0gaGFzIGJlZW4gdGhlIGluZHVzdHJ5J3Mgc3RhbmRhcmQgZHVtbXkgdGV4dCBldmVyIHNpbmNlIHRoZSAxNTAwcywgd2hlbiBhbiB1bmtub3duIHByaW50ZXIgdG9vayBhIGdhbGxleSBvZiB0eXBlIGFuZCBzY3JhbWJsZWQgaXQgdG8gbWFrZSBhIHR5cGUgc3BlY2ltZW4gYm9vay4gSXQgaGFzIHN1cnZpdmVkIG5vdCBvbmx5IGZpdmUgY2VudHVyaWVzLCBidXQgYWxzbyB0aGUgbGVhcCBpbnRvIGVsZWN0cm9uaWMgdHlwZXNldHRpbmcsIHJlbWFpbmluZyBlc3NlbnRpYWxseSB1bmNoYW5nZWQuIEl0IHc= –multipartBoundary Content-Disposition: form-data; name="json_detail" Content-Type: application/json;charset=utf-8 { "filename":"1478162481102.sdoc", "date_modified":"1478170365000", } –multipartBoundary– 我的问题是: 这个响应是以stream/文本表示的MultipartForm数据是正确的吗? 我怎样才能parsing响应的方式,它返回文件对象(在这种情况下是两个文件)? 我相信我不能使用中间件,因为这是一个外部的API调用。 我尝试了以下内容: pipe道到文件(这只是pipe道响应到一个文件) Http.get和busboy 我最后的select似乎是使用正则expression式parsing响应并写入文件。 这似乎是缓慢的 任何input将不胜感激! 谢谢!

http-proxy模块中的错误:必须提供一个合适的URL作为目标

我是Node.JS新Node.JS并在VPS上部署了第一个应用程序。 在8000端口上运行后,我决定创build一个http-proxy来将每个域转发到其特定的端口。 我喜欢这里的一个小应用程序: var http = require('http'), httpProxy = require('http-proxy'); var option = { router : { 'domain.com' : 'http://88.198.86.100:8000' } }; var proxyServer = httpProxy.createServer(option); proxyServer.listen(80); 88.198.86.100 is my server ip 。 所以,我的问题在这里显示,当我在我的浏览器电脑(谷歌浏览器)键入88.198.86.100 ,我的代理服务器应用程序被抛弃,并给出了这个错误: C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:119 throw err; ^ Error: Must provide a proper URL as target at ProxyServer.<anonymous> (C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:68:35) at Server.closure (C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:125:43) at emitTwo […]

NodeJs请求数据streamvs expressJs bodyParser

我正在使用0.12.2的一个非常旧的节点版本在NodeJS的遗留项目上工作 虽然它正在使用expressJS框架版本4.12.4与身体分析器版本1.8.1 一些旧的控制器利用从请求对象读取数据stream的本地nodeJS方法来访问请求的正文部分: req.on("data", function (data) { body += data; }); req.on("end", function () { console.log(body) // do something here with body } 较新的控制器使用bodyParser使用以下方法访问数据: req.body('something') 正文parsing器被实例化为: var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.json({limit: '50mb', extended: true})); 这是一个非常大的项目(1300+ Api)作为生产级软件运行。 我的问题是,以上是一个安全的做法? 会使用bodyParser影响本地请求对象? 我遇到的麻烦有时是遗留的控制器超时而没有返回任何响应,没有生成日志,因此无法debugging。

如何在下一个请求中从服务器获取数据回到服务器。

编写一个Node / Express应用程序。 当用户请求一个页面时,我把它发送给我的mongo文档数据,但是如果他们说去编辑页面,当我收到下一个请求时,我将失去所有这些珍贵的mongo数据,只能得到编辑表格中的内容,所以我失去了很多关于他们正在编辑的东西的信息。 有没有办法可以将整个文档与包含最初的mongo数据的请求主体一起发回?