admin管理员组文章数量:1323549
I'm programming a web application with AngularJS and I use NodeJS because of CORS problems. Well, when I'm trying to make the request in my node I get this error:
TypeError: https.request is not a function
at C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\index.js:56:22
at Layer.handle [as handle_request] (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\express\lib\router\route.js:137:13)
at cors (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:188:7)
at C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:224:17
at originCallback (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:214:15)
at C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:219:13
at optionsCallback (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:199:9)
at corsMiddleware (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:204:7)
at Layer.handle [as handle_request] (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\express\lib\router\layer.js:95:5)
My NodeJS file (index.js):
var express = require('express')
var cors = require('cors')
var bodyparser = require('body-parser');
var querystring = require('querystring');
var rootCas = require('ssl-root-cas/latest').create();
rootCas.addFile(__dirname + '/ssl/ovccatastromehes.crt');
const https = require('https').globalAgent.options.ca = rootCas;
var request = require('request');
var app = express()
app.use(bodyparser.urlencoded({extened:false}));
app.use(bodyparser.json());
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.get('/space/:id', cors(), function (req, res) {
var soapRequest = '<?xml version="1.0" encoding="utf-8"?>'
+'<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="/">'
+'<soap:Body>'
+'<Provincia xmlns="/"></Provincia>'
+'<Municipio xmlns="/"></Municipio>'
+'<SRS xmlns="/">EPSG:4326</SRS>'
+'<RefCat xmlns="/">'+req.params.id+'</RefCat>'
+'</soap:Body></soap:Envelope>';
var options = {
host: 'ovc.catastro.meh.es',
path: '/ovcservweb/ovcswlocalizacionrc/ovccoordenadas.asmx',
method: 'POST',
headers : {
"SOAPAction" : "",
"Content-Type": "text/xml; charset=utf-8",
"Content-Length": Buffer.byteLength(soapRequest)}
};
var httpreq = https.request(options, function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log("body: " + chunk);
res.send(chunk);
});
response.on('end', function() {
//res.send('ok');
});
});
httpreq.write(soapRequest);
httpreq.end();
});
app.listen(8100, function () {
console.log('CORS-enabled web server listening on port 8100')
});
The service where I call the node:
.factory("catastro", function($http, $sce) {
/*
* INICIALIZACIÓN DE VARIABLES
*/
var url = 'http://localhost:8100/space/';
var cat = {};
/*
* FUNCIONES
*/
// Obtener XML por referencia catastral
cat.leerReferenciaCatastral = function(rc) {
var urladd = url + rc;
return $http.get(urladd)
.then(function(respuesta) {
return respuesta.data;
});
};
return cat;
})
It's my first time with NodeJS and I'm a bit lost. I've searched about 'https' and request IS a function... Any suggestion?
I'm programming a web application with AngularJS and I use NodeJS because of CORS problems. Well, when I'm trying to make the request in my node I get this error:
TypeError: https.request is not a function
at C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\index.js:56:22
at Layer.handle [as handle_request] (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\express\lib\router\route.js:137:13)
at cors (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:188:7)
at C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:224:17
at originCallback (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:214:15)
at C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:219:13
at optionsCallback (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:199:9)
at corsMiddleware (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\cors\lib\index.js:204:7)
at Layer.handle [as handle_request] (C:\Users\Usuario\eclipse-workspace\AgriWea\WebContent\js\node_modules\express\lib\router\layer.js:95:5)
My NodeJS file (index.js):
var express = require('express')
var cors = require('cors')
var bodyparser = require('body-parser');
var querystring = require('querystring');
var rootCas = require('ssl-root-cas/latest').create();
rootCas.addFile(__dirname + '/ssl/ovccatastromehes.crt');
const https = require('https').globalAgent.options.ca = rootCas;
var request = require('request');
var app = express()
app.use(bodyparser.urlencoded({extened:false}));
app.use(bodyparser.json());
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.get('/space/:id', cors(), function (req, res) {
var soapRequest = '<?xml version="1.0" encoding="utf-8"?>'
+'<soap:Envelope xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:xsd="http://www.w3/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap/soap/envelope/">'
+'<soap:Body>'
+'<Provincia xmlns="http://www.catastro.meh.es/"></Provincia>'
+'<Municipio xmlns="http://www.catastro.meh.es/"></Municipio>'
+'<SRS xmlns="http://www.catastro.meh.es/">EPSG:4326</SRS>'
+'<RefCat xmlns="http://www.catastro.meh.es/">'+req.params.id+'</RefCat>'
+'</soap:Body></soap:Envelope>';
var options = {
host: 'ovc.catastro.meh.es',
path: '/ovcservweb/ovcswlocalizacionrc/ovccoordenadas.asmx',
method: 'POST',
headers : {
"SOAPAction" : "http://tempuri/OVCServWeb/OVCCoordenadas/Consulta_CPMRC",
"Content-Type": "text/xml; charset=utf-8",
"Content-Length": Buffer.byteLength(soapRequest)}
};
var httpreq = https.request(options, function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log("body: " + chunk);
res.send(chunk);
});
response.on('end', function() {
//res.send('ok');
});
});
httpreq.write(soapRequest);
httpreq.end();
});
app.listen(8100, function () {
console.log('CORS-enabled web server listening on port 8100')
});
The service where I call the node:
.factory("catastro", function($http, $sce) {
/*
* INICIALIZACIÓN DE VARIABLES
*/
var url = 'http://localhost:8100/space/';
var cat = {};
/*
* FUNCIONES
*/
// Obtener XML por referencia catastral
cat.leerReferenciaCatastral = function(rc) {
var urladd = url + rc;
return $http.get(urladd)
.then(function(respuesta) {
return respuesta.data;
});
};
return cat;
})
It's my first time with NodeJS and I'm a bit lost. I've searched about 'https' and request IS a function... Any suggestion?
Share Improve this question edited Jan 7, 2020 at 5:07 sideshowbarker♦ 88.4k29 gold badges215 silver badges212 bronze badges asked Oct 11, 2017 at 15:32 CristianCristian 351 gold badge1 silver badge8 bronze badges1 Answer
Reset to default 6That's because you're trying to call the function request
on the rootCas
object:
const https = require('https').globalAgent.options.ca = rootCas;
In other words, here you assign rootCas
to both require('https').globalAgent.options.ca
and https
.
Instead, try this:
const https = require('https');
https.globalAgent.options.ca = rootCas;
本文标签: javascriptNodeJS TypeError httpsrequest is not a functionStack Overflow
版权声明:本文标题:javascript - NodeJS. TypeError: https.request is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122360a2421776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论