Node.js v4.4.3 DNS resolution not working as expected - node.js

I have an application running on Oracle Linux 7 with Node.js v4.4.3. Our server has two DNS servers defined in resolv.conf. We Recently had the primary DNS server fail, so the secondary DNS server defined in resolv.conf should have worked. But Node.js failed to use the secondary server. It was almost as if it ignored the secondary and only tried to use the primary.
I thought it might have been an OS issue, so I manually modified the resolv.conf file and tested with DIG and it correctly used the secondary server, but when I tried with Node, it just wouldn't work. I wrote the program below to do some testing, basically modifying the resolv.conf while the program ran and nothing.
"use strict";
var dns = require('dns');
var express = require('express');
var bodyParser = require('body-parser');
var expressHandlebars = require('express-handlebars');
var cookieParser = require('cookie-parser');
var httpClient = require('request-promise');
//Initialize Express and Handlebars
var app = express();
app.enable('trust proxy');
app.use(cookieParser());
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
//Configure express app settings
app.engine('handlebars', expressHandlebars({extname: '.hbs'}));
app.set('view engine', 'handlebars');
app.get('/', function(req,res){
var options = { uri : 'https://someother/internal/api', json : true };
httpClient(options)
.then(function(data){
var success = { data : data };
console.log(dns.getServers(), 'Ok!');
res.json(success);
})
.catch(function(err){
var failed = { error : err.message };
console.log(dns.getServers(),err.message);
res.status(400).json(failed);
});
});
app.listen(8080, function(){
console.log("app started");
});
Any thoughts on why Node wouldn't re-read the resolv.conf or why it would not use the secondary DNS server when it realized the first was not working is appreciated.

You have roughly 3 options.
Add more application servers if your app is stateless
Lower the timeout in resolv.conf to 1 with rotate if you have multiple dns servers
increase the number of processes equal to the number of logical cores
You could do all three. This is really just a performance issue.

Node.js doesn't use the system's DNS resolution
https://nodejs.org/api/dns.html#dns_implementation_considerations

Related

how to redirect large file requests through node.js to a different server using request

I have a node.js server that is forwarding API requests through to another server on a different port (so that auth cookies and the like make it across), and this has all worked great until the client has needed to upload a large file (> 100mb).
When I try to do that, if the file is over ~30mb the request never even reaches the far server (which will happily accept large files when connected to directly), so I'm pretty sure it's dying in node ... somewhere.
"use strict";
const http = require("http");
const port = process.env.FRONTEND_PORT || 13370;
const path = require("path");
const request = require("request");
const express = require("express");
const app = express();
const staticPath = path.join(__dirname, "/dist/");
app.set("port", port);
// Attempt to set some ridiculously high limits on things
app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));
// Serve files from this location
app.use(express.static(staticPath));
app.use(
'/api',
(req, res) =>
{
var url = "https://localhost:12121/api" + req.url;
req.pipe(request(url)).pipe(res);
console.log(res.statusCode);
console.log(res.statusMessage);
});
// If we hit any path that doesn't exist, instead serve up index.html and let react router handle it
app.get("*", (req, res) => { res.sendFile(path.join(__dirname, "/dist/index.html")); });
app.listen(app.get("port"), () => { console.log("listening"); });
As you can see, I've tried bumping up the limits (based on other SO answers I've seen) using:
app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));
...but this doesn't seem to be helping me in this case. I'm sure I'm missing something obvious, but I am extremely new to node.js (and, honestly, web development in general).
(It's also worth noting, the file is being sent through a basic XMLHttpRequest as POST, sending a File object through - nothing fancy going on there)

Facing issues in loading mongo database and frontend app

I'm working on creating an anonymous discussion forum where I've deployed my MongoDB on MongoDB Atlas (cloud platform) using Heroku. The whole app is developed using React. Here is my code for server.js:
var express = require('express');
const path = require('path');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var models = require('./api/models/message');
var routes = require('./api/routes/routes');
var port = process.env.PORT || 3001;
var app = express();
var Message = mongoose.model('Message')
// Uncomment this line to run it on development mode (localhost) -- discussion is our db name //
// mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/discussion');
// This line is working on production mode //
mongoose.connect(process.env.MONGODB_URI || 'mongodb+srv://xxx:xxx#cluster0-xucmg.mongodb.net/test?retryWrites=true&w=majority');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
routes(app);
if (process.env.NODE_ENV === "production") {
app.use(express.static("frontend/build"));
console.log("production");
}
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'frontend/build', 'index.html'))
});
app.listen(port);
console.log('Server running on port ' + port);
My database name is discussion. When this line is uncommented:
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/discussion');
The database loads fine and frontend code also works fine on localhost.
But I'm trying to host my database on the cloud (to resolve issues with Heroku build) with this line:
mongoose.connect(process.env.MONGODB_URI || 'mongodb+srv://xxx:xxx#cluster0-xucmg.mongodb.net/test?retryWrites=true&w=majority');
I'm not sure why because of this line the frontend part is not being loaded (on Heroku deployment link). I assume maybe this is because of database not being loaded. But, it'll be a great help if anyone can help me figure out this issue. I tried to follow this solution: Connecting Heroku App to Atlas MongoDB Cloud service by providing whitelist access but the issue still persist.
I fixed this issue by updating the mongoose dependency version >= 5.0.0
Even after adding CIDR as 0.0.0.0/0 in IP whitelist in MongoDB Atlas, updating mongoose version will help to overcome this type of issue.

Express req.query always empty

I was using express route like this and I want my urls to contain query strings initially.
app.get('/', function(req, res){
res.render('index', {});
});
app.get('/us01', function(req, res){
console.log('query: '+JSON.stringify(req.query));
res.render('templates/us01', {});
});
app.get('/benchmark', function(req, res){
res.render('templates/benchmark', {});
});
However, I never get my query strings printed no matter what query strings I append after /us01. For example, "localhost:9200/us01?a=1" req.query should get me {a:1}, correct? Is this a common thing? What am I missing here?
My app.js
"use strict";
var express = require('express');
var expApp = express();
var http = require('http').Server(expApp);
var path = require('path');
var bodyParser = require('body-parser');
// all environments
expApp.set('port', process.env.PORT || 5555);
expApp.set('views', __dirname + '/views');
expApp.set('view engine', 'ejs');
expApp.use(bodyParser.urlencoded({ extended: true }));
expApp.use(bodyParser.json());
expApp.use(express.static(path.join(__dirname, 'public')));
//----------------ROUTES--------------------------//
require("./routes/route.js")(expApp);
http.listen(expApp.get('port'), function(){
console.log('Node-Server listening on port ' + expApp.get('port'));
});
My indexController.js has :
$stateProvider
.state('us01', {
url: '/us01',
templateUrl: '/us01'
}).state('benchmark', {
url: '/benchmark',
templateUrl: '/benchmark'
})....
This simple code:
const express = require('express');
const app = express();
app.get('/us01', function(req, res) {
console.log(req.query);
res.send("ok");
});
app.listen(80);
Then, accessed by http://localhost/us01?a=1 produces this output in the console:
{ a: '1' }
Or, if I use:
console.log('query: ' + JSON.stringify(req.query));
Then, I see this in the console:
query: {"a":"1"}
So, clearly something else is wrong in your code.
"localhost:9200/us01?a=1" req.query should get me {a:1}, correct?
It should get you query: {"a":"1"} if the code you show is running on port 9200 on localhost.
Is this a common thing?
No. Something other than the code you show is busted because there's nothing wrong with just that bit of code.
What am I missing here?
Things to examine:
Are you getting any output in the console when you hit any of your expected routes?
Can you prove that your server is running and your browser is hitting your route handlers?
If you just do console.log(req.query), what output do you get?
Are you absolutely sure that you've killed any prior servers and started the server that corresponds to the code you show. People sometimes get fooled by a prior version of the server that is still running and doesn't actually contain the code they think they are running.
Are you 100% sure you are running your server on the desired port that matches the port in the URL you are using.
When all else fails, sometimes a computer reboot will make sure no prior versions of anything are still running.

Bluemix Node.js work with session in multiple instances

I have a problem in my bluemix application when the project has two or more instances.
If I keep the project with only one instance, this code works as expected and when the url '/load' is called, I receive the data saved in the '/save'. But, when I put more instances in the application, the '/load' sends nothing is most times.
Its like the session is saved in one instance of the project and when the user hits another url, the '/load' is being executed in another instance.
So, does anyone knows how to make sure that the user only use one instance or share the session value between the instances?
var express = require('express');
var session = require('express-session');
var cfenv = require('cfenv');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(session({
resave: 'false',
saveUninitialized: 'true',
secret: 'cub1ksqu4d_mysp0t'
}));
var appEnv = cfenv.getAppEnv();
app.get("/save", function (req, res) {
req.session.fullname = "John Galt";
res.send("Saved session");
});
app.get("/load", function (req, res) {
res.send(req.session.fullname);
});
app.listen(appEnv.port, '0.0.0.0', function () {
console.log("server starting on " + appEnv.url);
});
You should design your app as stateless process. This is actually one of the 12 factors (see 12 factor app).
If you want to share a state between invocations and between instances of your app, a common practice is to use a database to store that data. There are frameworks to allow caching of data/states across the instances.
Seems that each instance it have their own request manager and it's not shared and each request is balanced through all instances.
You can make the /save to take a name parameter and test to execute /save a few times with different names and then the /load to see if it returns "randomly" the different names given.
If you want to share information through multiple instances I would recommend the use of a db service on your app.

Run NodeJS app on nodejitsu, all i get is "cannot get /"

as part of my learning i wanted to deploy my app to nodejitsu. Its running fine on my local server, but on nodejitsu all i get is
Cannot GET /
I thought it may have something to do with the NODE_ENV set to production on the server, but i never touched this on my local server. I changed it on nodejitsu to development but still i cant get it to work.
After commenting all the code i think the problem is in my index.js which i show below:
var express = require('express');//the framework
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//var session = require('express-session');
var methodOverride = require('method-override');
var passport = require("passport");//for authentication
var LocalStrategy = require('passport-local').Strategy;//local users for passport
var http = require('http');
var path = require('path');
var db = require('./dataBase');//to connect to the db
var userRoles = require('./routingConfig').userRoles;
var accessLevels = require('./routingConfig').accessLevels;
var debug = false;
var db = new db('inmobiliaria', 'localhost', 3306, debug);
require('./passport')(passport, db, debug)
var app = express();
app.set('port', 1337);
app.use(methodOverride());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }))
//app.use(session({ secret: 'SECRET' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
var role = userRoles.public;//default role
var username = '';
if (req.user) {
role = req.user.role;
username = req.user.username;
}
res.cookie('user', JSON.stringify({
'username': username,
'role': role
}));
next();
});
app.use('/', express.static(path.join(__dirname + '/../Client/')));
app.use("/lib", express.static(path.join(__dirname + '/../Client/lib/')));
app.use("/ctr", express.static(path.join(__dirname + '/../Client/Controllers/')));
app.use("/media", express.static(path.join(__dirname + '/../Client/media/')));
app.use("/views", express.static(path.join(__dirname + '/../Client/Views/')));
app.use("/srv", express.static(path.join(__dirname + '/../Client/Services/')));
app.use("/dct", express.static(path.join(__dirname + '/../Client/Directives/')));
require('./routes')(app, passport, db);
//require('./emailConfiguration');
http.createServer(app).listen(process.env.PORT || app.get('port'), function (err) {
console.log("Express server listening on port " + app.get('port'));
if (err) {
throw err; // For completeness's sake.
}
});
I investigated about this variable, but im not sure it has something to do with it. This is the url http://horaciotest.jit.su/, in case you want to see it.
Is this configuration? Am i doing something that should not be done?
Thanks for taking your time.
EDIT:
i managed to reduce the error case to a few lines i think. As the guys at nodejitsu suggested, im now trying to use the module node-static to serve static files, but i cant get it to work along express:
this code works on nodejitsu and my local server (or at least doesnt show any errors)
var statik = require('node-static');
var http = require('http');
var path = require('path');
var file = new (statik.Server)(path.join(__dirname + '/../Client/'));//index.html here
http.createServer(function (req, res) {
console.log(path.join(__dirname + '/../Client/'));
file.serve(req, res);
}).listen(8080);
but as soon as i add express, i get the error i mentioned above:
var express = require('express');
var statik = require('node-static');
var http = require('http');
var path = require('path');
var app = express();
var file = new (statik.Server)(path.join(__dirname + '/../Client/'));//index.html here
http.createServer(app, function (req, res) {
console.log(path.join(__dirname + '/../Client/'));
file.serve(req, res);
}).listen(8080);
Can someone tell me why when i add the express app i get the error? it may be what i need to get this to work on nodejitsu, thanks!
I found out what the problem was, hope it helps someone:
My project structure had two folders: one was named Client, where all my html and .js from angular where.
The other folder was WebServer, where i had all my nodejs files.
In order to deploy to nodejitsu, you run a command which is jitsu deploy, this in turn runs another command: npm pack. This command creates a .tgz file with all the data in you nodejs directory excluding the node_modules file and any file that starts with .. Problem is, if you like me have files outside that folder, they wont be included.
The solutions is to move your client folder inside the nodejs one. Everything you need to sent to the server should be in side this folder.

Resources