angular-sails websocket url undefined ( ws://www.undefined) - angularjs

I am using sails 0.10.5 and in angular frontend with angular-sails .
It gives an error like below on server side Whenever an api call by $sails.get
In My FrontEnd side controller code :
io.sails.url = 'http://localhost:1337';
io.socket.get('http://localhost:1337/admin/v1/user/subscribe', function (body, response) {
console.log('Sails responded with: ', body);
console.log('with headers: ', response.headers);
console.log('and with status code: ', response.statusCode);
});
and console is
Sails responded with: Object {}
app.js:167 with headers: Object {}
app.js:168 and with status code: 200
But in backend side sails console gives error like this
debug: --------------------------------------------------------
debug: :: Fri Dec 19 2014 10:44:25 GMT+0530 (India Standard Time)
debug: Environment : development
debug: Port : 1337
debug: --------------------------------------------------------
error: Error:
res.redirect() :: [socket.io] ::
Cannot redirect socket to invalid location :: 'ws://www.undefinedhttp://localhost:1337/admin/v1/user/subscribe'
at Object.Err.invalidRedirect (C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\sockets\lib\interpreter\interpret.js:33:14)
at ResStream.doRedirect (C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\sockets\lib\interpreter\interpret.js:310:31)
at C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\sockets\lib\interpreter\saveSessionAndThen.js:23:7
at C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\session\index.js:59:17
My route file is like this :
'get /admin/v1/user/subscribe': 'UserController.subscribe',
And in UserController.js :
subscribe: function (req, res) {
console.log('aaaaaaaaaaaaaaaaaaaa')
User.find().exec(function(err,users){
// subscribing this socket to User model classroom
User.subscribe(req.socket);
// subscribing this socket to User model instance room
User.subscribe(req.socket,users);
console.log('subscribe')
})
}

For setting up Socket connection with server:
Include sails.io.js in index.html.
Create a global variable for io.sails.url and you'll need to tell the Sails socket client what URL to connect to:
io.sails.url = 'http://localhost:1337';
Put this anywhere before you start making socket calls; the library is smart enough to wait until its connected before trying to make the calls.
To Check if Socket is connected,
io.socket.get('http://localhost:1337/data', function (body, response) {
console.log('Sails responded with: ', body);
console.log('with headers: ', sailsResponseObject.headers);
console.log('and with status code: ', sailsResponseObject.statusCode);
});
You should be able to see in the console whether or not the socket connected by looking for the "io.socket connected successfully." message.
The above code was about setting up socket connection as you requested.
Regarding your problem, Can you please provide some Code

The controller action you posted is clearly not being run (you would see the aaaaaaaaaaaaaaaaaaaa log in your console). Some other code is running and is calling res.redirect with invalid arguments. My first guess would be that you have a policy configured in config/policies.js that is attached to the UserController.subscribe action.

It was little silly mistake.
In my route.js , I am redirecting non WWW request to WWW request .
And socket request start with ws:// tag , thats why it is being also redirect to www.undefined
It is solved by a small condition
if(!req.isSocket){
// redirect logic
}else{
next();
}

Related

[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in nextjs

error - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (internal/errors.js:322:7)
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at NodeNextResponse.setHeader (E:\EndGame\FoodMart-client-side\node_modules\next\dist\server\base-http.js:101:19)
at DevServer.renderError (E:\EndGame\FoodMart-client-side\node_modules\next\dist\server\base-server.js:1094:17)
at DevServer.renderError (E:\EndGame\FoodMart-client-side\node_modules\next\dist\server\next-server.js:414:22)
at DevServer.run (E:\EndGame\FoodMart-client-side\node_modules\next\dist\server\dev\next-dev-server.js:451:35)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async DevServer.handleRequest (E:\EndGame\FoodMart-client-side\node_modules\next\dist\server\base-server.js:305:20) {
code: 'ERR_HTTP_HEADERS_SENT'
}
i can not understand from where the erron come in terminal but live site is okk.
In your Next.js application, check your pages/api/preview.js file. There is some code there that needs to be correct before the redirect will work
export default function handler(req, res) {
res.setPreviewData({})
res.redirect(req.query.redirect)
}
if you have something like the following your code won't work
res.end('Preview mode enabled')
I think you are using express. Somewhere in the code, you send the response after another response is sent. Make sure that you return the function after you call a res.send. All your res.send should have a return: return rsp.send({});

Cypress cy.request returns 404

I am using Cypress with react and node express server. I am trying to build a test that starts by calling cy.request with post method in order to initialize the state of the app for this particular test, but the response is always 404 as the following
Error:
CypressError: cy.request() failed on:
mysite.net
The response we received from your web server was:
404: Not Found
This was considered a failure because the status code was not '2xx' or '3xx'.
If you do not want status codes to cause failures pass the option: 'failOnStatusCode: false'
The request does work in prod/dev (status 200) but in cypress it always returns 404 as mentioned. I also checked the data structure of the request and it seems ok.
I am using the same route for all, the only difference is the method I use:
dev/prod: standard fetch API call seats under an apiHandler function.
Cypress: cy.request() method
My code:
cy.request({
method: 'POST',
url: `auth/login/example-url`,
failOnStatusCode: false,
form: true,
body: {
arrival: '2021-12-01T17:23',
departure: '2021-12-01T15:23',
destination: 'New York',
origin: 'Washington DC',
},
})
Thanks ahead for your help:)
when Browser: send request header and response status code OK
Using cy.visit(): response status code Not Found
So the problem is the Request Headers, you better check it, and set Headers in cy.visit() to look like Browser Headers. you'll receive the status code OK.

how to fix '403 (Forbidden)' on a $http.get request to etherpad

[header][1]
[header][2]
i am trying to send a get request to etherpad hosted in a different domain. the OPTIONS preflight request goes through but the get request returns 403 status.
however when i install CORS for chrome extension the request goes through
when i install cors chrome extension the request goes through
here is my code:
/* ........ export draft to html/pdf/word ........ */
//get content for this template
var url = Drafting.domain + Drafting.port + '/p/' + Drafting.padID + '/export/html?' + 'apikey=' + Drafting.apikey;
$http.get(url).then(function(response, status){
if(typeof(callback)==='function' && callback)
{
callback(response.data);
}
});
```}
[1]: https://i.stack.imgur.com/25dsj.png
[2]: https://i.stack.imgur.com/ivHnn.png
i just passed 'withCredentials:true' together with the url and then modified the importexport.js inside the etherpad files where 'AccessControlAllowOrigin' was set to '*' i replaced it with a specific domain and it worked.

Can't get any response back while calling service through frisbyJS

The code I am using is:
var frisby = require('frisby');
frisby.create('Get paf data')
.get('https://services.....')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.toss();
The error I get while running it from CLI using
jasmine-node EndpointTest_spec.js
The error I get is:
Error: Expected 500 to equal 200
Error: Header 'content-type' not present in HTTP response
So do I need to first load my website and call services through my application and then run frisby ?
But then it defeats the purpose of just making a quick check for all the endpoints used in application without running it.
You are calling request with https:// which may be secure server so use
{ strictSSL: false} in your get method .get('https://services.....',{ strictSSL: false}). It will solve your problem.
var frisby = require('frisby');
frisby.create('Get paf data')
.get('https://services.....',{ strictSSL: false})
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.toss();

How to properly connect cloudant + sag

I'm working in getting a connection to cloudant done.
The following is using sag library for php:
<?php
header('Content-Type: text/html; charset=utf-8');
require_once('../../src/Sag.php');
//this credentials are from API key
$uName="";
$pName="";
$sag = new Sag('user.cloudant.com');
$sag->login($uName, $pName);
$sag->setDatabase('test');
try {
$result = $sag->get('/test/_design/wordsP/_view/errores');
echo ($result);
}
catch(Exception $e) {
error_log("Something's wrong");
var_dump($e);
}
?>
However I'm not getting expected result (). The view does work, if used just in the url bar.
The response is:
object(SagException)#3(6){
[
"message:protected"
]=>string(50)"Sag Error: cURL error #7: couldn't connect to host"[
"string:private"
]=>string(0)""[
"code:protected"
]=>int(0)[
"file:protected"
]=>string(73)"/home2/.../public_html/clant/src/httpAdapters/SagCURLHTTPAdapter.php"[
"line:protected"
]=>int(134)[
"trace:private"
]=>array(3){ .............
Is there something I'm not using corretly in the php script? (removed current password + username as well as account, but they're there).
Curl error 7 means you are unable to connect to the host or proxy:
CURLE_COULDNT_CONNECT (7)
Failed to connect() to host or proxy.
Source: http://curl.haxx.se/libcurl/c/libcurl-errors.html
When you connect to the URL from your browser if your browser is using a proxy, you will also need to configure sag to use that proxy.
Also, when you say that you replaced the account in your code example, did you mean that you replaced user in $sag = new Sag('user.cloudant.com'); with your actual username? If not, you will need to use your actual username.

Resources