Having trouble writing remotely to Cassandra server from node.js - node.js

Environment: OS: Debian, Cassandra: 3.10, Node.js 7.5.0,cassandra-driver: 3.2.0
Error:
(node:7484) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: All host(s) tried for query failed. First host tried, 192.168.10.151:9042: Error: connect ECONNREFUSED 192.168.10.151:9042. See innerErrors.(node:7484) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Nodejs Code:
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var fs = require('fs');
var cassandra = require('cassandra-driver');
var async = require('async');
var client = new cassandra.Client({contactPoints: ['192.168.10.151'], keyspace: 'users'});
router.post('/', function(req, res, next) {
var query = 'INSERT INTO userAuth(email,password) VALUES(req.body.email,req.body.password)';
client.execute(query);
});
module.exports = router;
I am not sure what i am doing wrong.

your insert statement also doesn't seem to be valid. Can you please try with
'INSERT INTO userAuth(email,password) VALUES(?,?)'
and then
execute(query, [req.body.email, req.body.email]);
plus I think ti might be a good idea to add function for query result handling
function(n, row) {
console.log('everything is o.k.');
},
function (err) {
console.log('something went wrong', err);
}
Also the error seems to be very similar to this one, so try to update the broadcast address in the cassandra.yaml file to 192.168.10.151
dse-driver connection refused

Are you sure you don't have cassandra user?
If you do have one, connect to cassandra this way:
const authProvider = new cassandra.auth.PlainTextAuthProvider('user', 'pass');
const client = new cassandra.Client({ contactPoints: ['192.168.10.151'], keyspace: 'users', authProvider: authProvider});

Related

Intermittent (time dependent) error when using Node Package rate-limiter-flexible in Express Node.js application

My overall goal is to apply the basic logic of the package "rate-limiter-flexible" for my Express Node.js application. I've gotten the basic functionality with the "Memory" version which does not require any communication with a database (IP address counts stored in Node server memory). Now I'm trying to get the functionality to work with the MongoDB rate limiter modules (using an instance of rateLimiterMongo object from the package).
The problem I'm encountering is that my rate limiter middleware is throwing an error intermittently... The only pattern I can find is that the error occurs more frequently if there are > ~10 seconds between requests to the Node app. This is the error which occurs every 5-10 requests:
[RateLimiter Error]: Rejection object:
TypeError: Cannot read properties of null (reading 'points')
at RateLimiterMongo._getRateLimiterRes (C:\root\node_modules\rate-limiter-flexible\lib\RateLimiterMongo.js:124:33)
at RateLimiterMongo._afterConsume (C:\root\node_modules\rate-limiter-flexible\lib\RateLimiterStoreAbstract.js:51:22)
at C:\root\node_modules\rate-limiter-flexible\lib\RateLimiterStoreAbstract.js:205:16
at processTicksAndRejections (node:internal/process/task_queues:96:5)
So far, I have tried:
Disabling buffering with Mongoose (was a recommendation from the package docs) -- did not work
Changing from MongoDB Atlas free tier to a locally hosted MongoDB instance -- this resolved all occurrences of the error, but I need to be able to use the cloud service
Here is a minimal reproduction of the error I'm facing when connecting to a MongoDB Atlas free tier cluster (via MONGO_DB_URL):
// Node packages:
require('dotenv').config();
const { RateLimiterMongo } = require('rate-limiter-flexible');
const mongoose = require('mongoose');
const express = require('express');
const app = express();
// open a Mongoose connection and save it:
const dbUrl = process.env.MONGO_DB_URL;
const connectDB = async function () {
await mongoose
.connect(dbUrl, {
// options
})
.catch(error => {
console.log("DB not connected!");
// handle error here (initial connection)
});
};
connectDB();
const mongoConn = mongoose.connection;
// options and passing to the RateLimiterMongo constructor:
const opts = {
storeClient: mongoConn,
points: 3, // Number of points
duration: 1, // Per second(s)
};
const rateLimiterMongo = new RateLimiterMongo(opts);
// create the middleware for the express app:
const rateLimiterMiddleware = (req, res, next) => {
rateLimiterMongo.consume(req.ip, 1)
.then((rateLimiterRes) => {
console.log("[RateLimiter Success]: RateLimiterRes object:\n", rateLimiterRes);
next();
// Allowed
})
.catch((rej) => {
console.log("[RateLimiter Error]: Rejection object:\n", rej);
res.status(500).send("RateLimiter error(s)...");
// Blocked
});
};
// Express app code:
app.use(rateLimiterMiddleware);
app.get('/', (req, res) => {
res.status(200).send("Valid Route!");
});
app.listen(3000, () => {
console.log(`Serving on port 3000!`);
});
Thanks all for any help you can provide with this. It may just be a side effect of using the MongoDBAtlas free tier...
Most likely, you use mongoose v6.x, which changes how connection established. It returns promise, which should be awaited, before connection can be used to make queries. More context in migration to 6 guide.
You should await connectDB() call and only then create middleware. In other words, your Express app should wait until connection to MongoDB Atlas is established. You can read through comments in related closed issue on github.

504 on Node.JS frontend Azure AppService calling C# API backend Azure AppService

We have a frontend using ExpressJS server and talk to a backend on .NET 5. Both frontend and backend are running on separate Azure AppService.
FE: https://my-front-end.azurewebsites.net
BE: https://my-back-end.azurewebsites.net
Whenever we try to call the backend from frontend, it will always return 504 Gateway Timeout.
We try to add a simple /hello endpoint on the FE side and we could see {"message":"Hello World!"} is printed out. But the other endpoints, for example api/vessels/3 will get 504 - Gateway Timeout
const dotenv = require('dotenv');
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const bodyParser = require('body-parser');
// import path from 'path';
dotenv.config();
const app = express();
const port = process.env.PORT || 1337; // default port to listen
app.use(cors());
app.use(function(_, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
app.use(bodyParser.json());
app.use(express.static(process.cwd() + '/ui/build/'));
// define a route handler for the default home page
app.get('/', (_, res) => {
res.sendFile(process.cwd() + '/ui/build/index.html');
});
app.get('/hello', (_, res) => {
res.status(200).send({ message: "Hello World!" });
});
const getHeaders = (domain = 'ABC') => {
return {
'Content-Type': 'application/json',
'cp-site-domain': domain
}
};
const http = axios.create({
baseURL: process.env.API_API_URL,
timeout: process.env.REQUEST_TIMEOUT
});
app.get('/api/vessels/:orgId', async (req, res) => {
const { orgId } = req.params;
const { data } = await http.get(`/Vessels?organizationId=${orgId}`, {
headers: getHeaders()
});
res.status(200).send(data);
});
// start the Express server
app.listen(port, () => {
console.log(`server started at http://localhost:${ port }`);
});
The error log from iisnode is:
(node:9880) UnhandledPromiseRejectionWarning: Error: connect EACCES 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)
at TCPConnectWrap.callbackTrampoline (internal/async_hooks.js:130:17)
(node:9880) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:9880) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
There is no problem if the backend APIs are being called directly from the UI (the usual AJAX). The error only happen when the request to BE is being triggered by ExpressJS.
Do we need to configure something on NodeJS or on Azure AppService side?
I found the solution to this problem.
It turns out, the problem is related to the environment variable. On the local development, we are using dotenv that loads environment variables from a .env file into process.env.
The value of the environment variables are not loaded when we deploy the NodeJS into Azure AppService.
So, we need to add the environment variables into the AppService > Configuration and restart the app.

PATCH route/endpoint in Express not working

Hi I've written the following route for an api endpoint which isn't working. When I test with Postman and my code, it's simply a 404 not found error.
router.patch("/favorite", async (req, res) => {
user = await User.findById(req.body.id)
if (user == null) {
return res.status(404).json({ message: 'Cannot find user' })
}
if (req.body.putArr != null) {
res.user.favPokemon = req.body.putArr;
}
try {
const updatedUser = await res.user.save();
console.log(res.user.favPokemon);
console.log(updateUser);
res.json(updatedUser);
} catch (err) {
res.status(400).json({ error: err.message });
}
});
What am I missing/what error do I have in my code? For reference, here's my mongoDB setup for users:
Edit: Apologies for not specifying the endpoint. To be more clear, the end point and code calling this is:
const favThis = async (e) => { // Patch method to favorite or unfavorite a pokemon
debugger;
e.preventDefault();
try {
console.log(putArr);
const newUser = {userID, putArr};
await axios.patch("http://localhost:5000/users/favorite", newUser);
} catch(err) {
err.response.data.msg && setError(err.response.data.msg)
}
};
, so it's http://localhost:5000/users/favorite. I have other endpoints working fine such as http://localhost:5000/users/login and http://localhost:5000/users/register, and inside server.js I have app.use("/users", require("./routes/users"));
Additionally, server.js is simply
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
require("dotenv").config();
// set up express
const app = express();
app.use(express.json());
app.use(cors());
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`The server has started on port: ${PORT}`));
// set up mongoose
mongoose.connect(
process.env.MONGODB_CONNECTION_STRING,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
},
(err) => {
if (err) throw err;
console.log("MongoDB connection established");
}
);
// set up routes
app.use("/users", require("./routes/users"));
app.use("/todos", require("./routes/todo"));
Edit 2:: I notice now that when I test on Postman, it's an infinite loop and the call is hung. I also get the following warnings in my console:
(node:36447) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
and
(node:36447) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Based on the warning you have failing code inside a promise which is not being caught. Perhaps in this line user = await User.findById(req.body.id).
Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

Stuck in loading with mongoose

I am trying to create a web-application with node.js, mongoose and MongoDB,
I am trying to load the web-page localhost:8800/api/auth/register which is stuck at loading since past 15 minutes.
VS Code Terminal return the following :
(node:2908) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (E:\Projects\Applications\chitter-chatter\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:2908) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:2908) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (E:\Projects\Applications\chitter-chatter\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(node:2908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
My Scripts are as following :
Index.js :
const application = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");
const userRoute = require("./routes/users");
const authRoute = require("./routes/auth");
dotenv.config();
mongoose.connect(process.env.MONGO_URL, {useNewUrlParser: true, useUnifiedTopology: true}, () => {
console.log("connected to MongoDB")
});
// middleware
application.use(express.json());
application.use(helmet());
application.use(morgan("common"));
application.use("/api/users", userRoute);
application.use("/api/auth", authRoute);
application.listen(8800, () => {
console.log("backend server is running!")
})
Auth.js :
const User = require("../models/User");
// REGISTER
router.get("/register", async (req, res) => {
const user = await new User ({
username: "john",
useremail: "john#gmail.com",
userpswrd: "123456"
})
await user.save();
res.send("oK")
});
module.exports = router
I am also using .env for MONGO VIA URL CONNECTION
Sorry for the bad writing apologies in advance also I am new to this so pls correct me! I know i have done a lot of mistakes,
Thanks for u're sincere time dedication and sympathy
First of all, you must make sure that you are connecting to the database without any error.
To do this, start listening on connect's callback function:
try {
// This configuration is better
mongoose.connect(process.env.MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
}, err => {
if (err) throw Error(err.message);
console.log("connected to MongoDB");
application.listen(8800, () => console.log("backend server is running!"));
});
} catch (error) {
console.log(error);
}
I think you should make this operation in a try-catch statement like this:
// REGISTER
router.get("/register", async (req, res) => {
try {
// Create user in database
const user = await User.create({
username: "john",
useremail: "john#gmail.com",
userpswrd: "123456"
});
res.status(200).json({ success: true });
} catch (error) {
res.status(500).json({ error });
console.log(error);
}
});
module.exports = router;
Then you can see error's details, and server keeps running.
If you can't solve the problem just add a comment here I'll be back ASAP
check your router or wifi if you are using to run mongoose
try with your mobile internet

ERROR: TypeError: Parameter "url" must be a string, not function

I am trying to set up my node js application to connect to a MongoDB Atlas database, I am using the full driver code which they have provided me with. But when I start the app I receive the following error:
(node:4195) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Parameter "url" must be a string, not function
Here is the code that I have for my app.js:
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
method_override = require("method-override");
// mongoose.connect("mongodb://localhost/art_eng");
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(method_override("_method"));
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://boris:<passwordWasHere>#arteng-jvhbz.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
And the portfolio page that I have that is connected to the database cannot load and times out. How can I fix it?
I was also facing the same issue but resolved.
Your password must not contains any character which is problematic while parsing.
In my case it just solved by removing % character.

Resources