Disabling Meteor 2 minute server timeout? - node.js

How can one remove/increase the server side connection timeout from Meteor when using server side routes with Iron Router?
There is a fix when using vanilla Node: https://contourline.wordpress.com/2011/03/30/preventing-server-timeout-in-node-js/
But I cannot figure out where to put the code in the above sample to make it work with Meteor and Iron Router
Here's a sample route:
Router.route('veryslowroute', {
path: '/veryslow',
where: 'server',
action: function () {
// Route never rendered in browser. Reducing value here to 110000 will render just fine
Meteor._sleepForMs(120000);
// These I've tried:
// this.response.setTimeout(0);
// this.response.connection.setTimeout(0);
// this.response.connection.server.setTimeout(0);
this.response.writeHead(200, {'Content-Type':'application/json'});
this.response.end(JSON.stringify({
key: 'Sorry for being so slow'
}));
}
});
So it appears the timeout needs to be set somewhere else. Anybody?
EDIT: It appears that this isn't about NodeJS but Meteor itself, likely the webapp-package. Still couldn't find a workaround. Same thing happens when using webapp directly without Iron Router.
EDIT: Now there is an issue in Meteor: https://github.com/meteor/meteor/issues/3826

Related

Issue in connecting to heroku postgres from node + express + also locally

New to Node here, trying for the last 3 days straight, no clue.
Read all similar issues and tried literally everything I could find, no luck.
Suspecting something that is not common or related to my machine or code.
Issue: trying to fetch data in node.js from postgres db - to console (at least) so to render it later as HTML
Database has table name: students on heroku, and has records
Also locally on my macOS, I have postgres installed with simple data in a table called students
I couldn't fetch any data, no error, no idea how to track it!
Tried creating connection with pool, client.. also used heroku guide here exactly
Literally everything that other users mostly encountered
DATABASE_URL environment variable is ok, if i echo $DATABASE_URL in Terminal:
postgres://xnlkikdztxosk:kuadf76d555dfab0a6c159b8404e2ac254f581639c09079baae4752a7b64a#ec3-52-120-48-116.compute-1.amazonaws.com:5432/uytnmb7fvbg1764
When i run 'node app.js' server starts ok on port 3000, I can use postman on the root '/' OK and it works, it returns back the json info and console.log
If i try postman to '/students' then it tries forever, no results, no error, nothing
Tried also with my local installation of postgres, same thing
My modules are ok, and I run npm install several times
Thought could be my mac firewall, i turned it off completely
Also tried this, nothing prints out or no idea where to track it:
process.on('uncaughtException', function (err) {
console.log(err);
});
Guide or steps to follow in order to track issues like this will be highly appreciated
app.js file:
const express = require('express')
const bodyParser = require('body-parser')
const { Client } = require('pg');
const app = express()
const PORT = 3000
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
client.connect();
app.get('/', (req, res) => {
res.json({ info: 'Info: This is the root directory' });
console.log('main directory')
})
app.get('/students', (req, res) => {
client.query('SELECT * FROM students;', (err, res) => {
if (err) throw err;
for (let row of res.rows) {
console.log(JSON.stringify(row));
console.log('WHOOOOOO, finally!');
}
client.end();
});
});
app.listen(PORT, function(){
console.log('Server running on port 3000');
});
Well, my node version was for some reason v14, not sure how that happened, but the most stable version in node site is 12, so I installed v12 and the connection to pg worked locally and remotely on heroku.
This is just to highlight what worked with me after trying 4 days straight.
However, that may trigger for you different issue like like this which I'm facing:
DeprecationWarning: Implicit disabling of certificate verification is deprecated and will be removed in pg 8. Specify `rejectUnauthorized: true` to require a valid CA or `rejectUnauthorized: false` to explicitly opt out of MITM protection.
All answers found so far point to: pg module already fixed in v7.18.1 but for some reason I can't force package.json to take that version, it jumps me to version 7.18.2
Tried that along with latest version 8.3 same issue with heroku, but locally the message doesn't show
Not big deal though, connection works for now until figuring it out.
I think the issue here is that you don't send back any response in the /students route .Notice the / route u have a res.json which sends back a response but in /students route i don't see where your response is sent and that's why you wait forever

Fetch gives empty response while waiting for long period

I have app made with react and node .
The react app needs to make api call to the node app the node app is running on port 5100 . I am facing problem where I get net_err empty response in the console after waiting long period of time . The thing is my api takes 200s to get the response from the server .
When I hit
http://localhost:5100/api/users/wait-ip
I get response after 200 second But when I hit this in the react app
fetch('/api/users/wait-ip')
I get the following error on console
GET http://localhost:3000/api/users/wait-ip net::ERR_EMPTY_RESPONSE
This is my function for api
router.get('/api/users/wait-ip',(req,res)=>{
//Others things happen here
setTimeout(()=>{
return res.json({
data:1
})
},150000)
})
This is the response I get while hitting directly on browser after 150seconds
Any help on how to solve this will be appreciated
Using Node.js API with React is a common use case. I think the reason you are facing the issue in getting response is that you are using fetch call synchronously. Always use async/await for it.
async function getUsers() {
let response = await fetch('/api/users/wait-ip');
let users= await response.json();
//...
return users;
}
Using the function:
getUsers().then(result => {console.log(JSON.stringify(result));});
Hope that helps.
For me the problem was on both client and the server .
I have read some of the post on how to fix timeout on the server . One of them was
const server = http.listen(port, () => console.log(`Server running on port ${port}`));
server.timeout = 200000;
Well this worked but only for the direct browser call .
For the asynchronous call I need to set it for each function where I wanted it like this
router.get('/wait-ip',(req,res)=>{
req.timeout =160000;
setTimeout(()=>{
return res.json({
data:1
})
},150000)
})
And for client part with pxoy it didn't work properly . So what I did was posted the full url
fetch('http://localhost:5100/api/users/wait-ip')
I hope this helps with other person too
I may be because of the Cross Origin Resource Sharing (CORS) header. Usually calling from a browser there is an "OPTION" call that is made followed by, in this case, the "GET".
I would try
fetch('/api/users/wait-ip', {
mode: 'no-cors' // 'cors' by default
})
If this fixes the problem either you force not to use cors on the client side. Or the server should manage it. Another option is to allow the proxy to set these headers.
ref: https://developers.google.com/web/ilt/pwa/working-with-the-fetch-api
section: Cross-origin requests
This is an API problem. I'd make set breakpoints in your API and make sure the right fields are populated for the response.
Node.js Debugging Guide...

how to use sails.io.js version 0.11.3 in node server

I would like to create a chat application in my sailjs based project, How and where can i configure the socket and related setting in the server ? I got a sample project from this repository but it is using sails v0.10 and i need to use sails v0.11.3, but v0.11.3 having many changes while using socket in Nodejs script, for example, We can't use onConnect since it is deprecated.
I have tried this example, but not working this with sail v0.11.3
https://github.com/sgress454/sailsChat
This is the code i have done, but i don't know where should i put , it is not working when i put this in sockets.js file with in the config directory
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:9002';
// ...
io.socket.on('connect', function socketConnected() {
console.log('connect..');
});
// Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/hello', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
io.socket.disconnect();
// (note that there is no callback argument to the `.disconnect` method)
});
Please guide me on this.
Issue has been solved after using the main repository in this link
https://github.com/balderdashy/sailsChat

Hapi.js with Socket.io -- Where is socket.io.js?

I'm trying to hook in socket.io to a Hapi.js server. I've tested the socket.io implementation in vanilla Node.js and everything works great; the server side of the Hapi implementation seems to work fine, but the "/socket.io/socket.io.js" resource is not served to the client.
I've checked the Hapi example, but they only show what to do on the server, and their documentation on the client handshake seems odd: they have a server on port 8000 but say to post for the socket.io handshake to 8080--I've even tried this (which seems wonky and inconsistent with every other socket.io implementation) with no luck.
Thanks!
Hapi 8 has introduced some new intricacies so if anyone if revisiting this issue...
On the client, you don't have to use /socket.io/socket.io.js, instead use
a cdn (https://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js)
or serve the file more specifically (http://yoursite.io/js/socket.io/socket.io.js)
Better yet, use something like npm/browserify and (https://www.npmjs.com/package/socket.io-client):
var socket = require('socket.io-client')('http://localhost:8080');
Which ever way you choose to include the client side code above
Hapi 8 will allow you to do something cool on your server with chat like this:
server.js
server.connection({ port: 8000, labels: 'app' });
server.connection({ port: 8080, labels: 'chat' });
server.register({
register: require('./server/plugins/socketIO')
},
function(err) {
if (err) throw err;
});
/plugins/socketIO/index.js
exports.register = function(server, options, next) {
var io = require('socket.io').listen(server.select('chat').listener,{log:false});
io.sockets.on('connection', function (socket) {
socket.on('someAction', function(name, cb) {
...
});
...
});
}
Answer: Load the Client Script from CDN
To answer your specific question: we decided to load the socket.io.js script from the CDN (e.g: http://cdnjs.com/libraries/socket.io ) to make our app load faster. see: index.html#L23
Working Offline ...? (Or Prefer Not to use CDN?)
If loading the client script form CDN is not an option because you are on dial-up or working offline, then use inert and expose the file to your app. e.g:
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({
host: '0.0.0.0',
port: Number(process.env.PORT || 3000)
});
// uses https://github.com/hapijs/inert to serve static files
server.register(require('inert'), function () {
server.route([
{ method: 'GET', path: '/', handler: { file: "index.html" } },
{ method: 'GET', path: '/socket.io.js', handler: { file: './node_modules/socket.io-client/socket.io.js' }
]);
server.start(function () {
console.log('Visit: http://127.0.0.1:'+server.info.port);
});
});
module.exports = server;
The client file is located in: your_project/node_modules/socket.io-client/socket.io.js
Up-to-Date Solution/Example with End-to-End Tests
If you are still looking for an example of how to use Socket.io in a Hapi.js app we created a complete one (with documentation/comments & end-to-end tests).
see: https://github.com/dwyl/hapi-socketio-redis-chat-example
Its up-to-date with the latest versions of Hapi & Socket.io and uses Redis Pub-Sub to persist and distribute chat messages.
You can try to copy the socket.io.js file out from the node.js directory to a well-known directory.
I would first confirm that you don't have any firewall issues interfering with the serving of the request.
If a firewall isn't responsible for blocking the request make sure that the src for the javascript file is pointing to the same server and port number, as indicated on the Hapi.createServer line, that you have configured.
If the request reaches the server it will output a debug line in the terminal indicating that it served the file.

Unable to solve EADDRINUSE in NodeJS

I am trying to integrate the mailchimp OAuth plugin for node (https://github.com/gomfunkel/node-mailchimp/) and I keep getting an EADDRINUSE error and I am not sure what the issue is. I know what the error means I just don't know how to stop it.
My express server is running on port 3000. The mailchimp class evidently wants to spawn a server - it defaults to port 8100. I have tried changing both of these ports to no avail. What could be the issue?
Here's the route code I am using:
exports.test = function(req, res){
var MailChimpOAuth = require('mailchimp').MailChimpOAuth;
var MailChimpAPI = require('mailchimp').MailChimpAPI;
var options = {
clientId: '00000000',
clientSecret: 'abcdefghijklmnop',
serverUri: 'http://localhost',
redirectUri: 'http://localhost',
};
var oauth = new MailChimpOAuth(options);
Now my initial request works and any subsequent ones cause the error - probably encountering the already spawned server. Is that a bug in the mailchimp class that is should see if it already spawned?
From what I can tell, you're creating a new MailChimpOAuth on every request, so when your second request rolls around, you've already got a server listening on 8100.
You need to do all that setup stuff once at initialization and, in your request handler, refer to the already instantiated MailChimpOAuth instance.
As a general rule, if you find yourself doing a require(...) in code that runs more than once, your logic needs rethinking.
just run:
killall -9 node
and try it again. It worked for me

Resources