Web-based MongoDB (Mongo-express) how to use middleware - node.js

I'm new at nodejs and I gues therefor I can't get to it.
I'm trying to install https://github.com/mongo-express/mongo-express as middleware (I got it to work as standalone app).
I did those three steps
var mongo_express = require('mongo-express/lib/middleware')
var mongo_express_config = require('./mongo_express_config')
app.use('/mongo_express', mongo_express(mongo_express_config))
And after that not sure how to reach it(how do I run it). I hoped for the port 8081 to be active but it's not.
Can anyone help me and explain how this supposed to work?

Related

looking for the best way to connect to an SSH server with ReactJS

I have a website running on Node built from ReactJS and I want to have an option to send a file to an SSH remote server by entering IP, username and password.
I have been looking at large number of options but kept getting errors. I tried npm i ssh2 and node-ssh but both doesn't seem to be working well with React..
The simplest thing I tried was building a fresh website using npx create-react-app and adding node-ssh to it. I then tried adding the simple line of code
const { NodeSSH } = require('node-ssh');
const ssh = new NodeSSH();
But I kept getting these errors and after a lot of digging it seems like this is not the right way of approaching this.
This is why I need your help.. I believe there has to be a way to access SSH server from a node webserver..
Thank you

Unable to connect to Express welcome page after install

I am a total newbie and have a problem I just can't find the answer to. I followed the steps on this page: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/development_environment and I'm not able to connect to the sample pages with express. The hellonode.js connects fine when I run that.
Both of the express examples connect when I start the server and I get the 'example app listening...' message but when I go to the page in any browser I get a connection refused err. I thought maybe it could be firewall related so I disabled that, but that didn't help. I also tried going through the steps multiple times with different directory names, tried changing the port numbers and tried rebooting the machine. I always get the same result.
At this point I think I've just run out of talent and can't think of anything else to try. When I look at the package.json files they match what is shown on the page. Hopefully I'm missing something obvious, I just can't see it.
Edit:
The code I've used is from the page....first one is:
const express = require('express')
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.listen(8000, () => {
console.log('Example app listening on port 8000!')
});
and the second spot I tried and got the listening message but couldn't connect via a browser was after installing the express application generator and then running:
DEBUG=helloworld:* npm start
Edit 2:
If anyone else ever has this problem, I got it to work by following the instructions on this page instead. https://learn.microsoft.com/en-us/windows/nodejs/beginners#types-of-nodejs-applications Now to figure out why...
Hello Nick and welcome to Stack Overflow!
What you did is probably installing the packages you had missing befor with npm install.
Or maybe you were not using the correct syntax to set that environment variable DEBUG on your machine.
If I can suggest, avoid using express-generator, it's much better to learn how to do that manually, it's just a couple of lines of code anyways!

Azure Functions crashing loading MongoDB in Node

I'm attempting to write an Azure Function, in Node, to connect into a MongoDB instance (Cosmos DB in this case).
However, as soon I run require("mongodb"), my function crashes, without throwing an error, or logging anything, with the HTTP response returning a 502 code.
My setup:
Creating a function app using all defaults through the Azure portal.
Creating a package.json with mongodb version 3.x.
Running npm install through the Kudu shell
Include the require statement in my code.
Make a request to the function
This doesn't throw an error in the code, and I see logging that's run before, but not after the require statement (which is making it pretty difficult to debug).
I've also tried following through this guide about running a mongo query from a function, and it fails in exactly the same way for me.
After putting some hooks into Node's module module, my attempts to debug this led to a line in one of mongo's dependencies that fails in a similar way when run in isolation (from saslprep), which seems to stem from running out of stack space.
However, this feels like its a pretty mainstream use for an Azure function, and I haven't seen any similar issues, so I'm inclined to suspect that its an issue with my setup, rather than the mongodb library, but I haven't been able to find a misconfiguration, as I haven't changed any defaults - right now, I'm stumped!
I don't have a full code example right now, as I'm away from my work computer, but the code is something like
const mongo = require('mongodb');
module.exports = function(context) {
context.res = {
body: 'Hello world'
};
context.done();
}
Without the require statement, the code runs fine, returning the response to the browser.
It turns out that this problem was caused by running out of stack space. After pushing a patch to the saslprep library (v1.0.1), this has now been fixed.
Im pretty sure that if you add to your require function the same as in Microsofts Cosmos DB guides for mongo the following it should work
var mongodb = require('mongodb').MongoClient;
you have it as:
const mongodb = require('mongodb');
Im curious to know if that makes a difference. After looking through Microsofts own docs nearly all of them are declared that way.
Here is the tutorial I found: MongoDB app using Node.js

How to separate express server code from Express business logic code?

All the Node.js tutorials that I have followed have put everything in one file. It includes importing of libraries, routing, database connecting and starting of the server, by say, express.js:
var app = require('express');
app.get('/somePath', blah blah);
app.listen(...);
Now, I have 4 node servers behind an Nginx load balancer. It then becomes very difficult to have the source code updated on all the four servers.
Is there a way to keep the source code out of the server creation code in such a way that I can deploy the source code on the servers as one package? The server creation code should not know anything about routing or database connections. It should only be listening to changes in a folder and the moment a new module meta file appears, it starts hosting that web application.
Much like how we deploy a Java code packaged as war by Maven and deployed to the webapp of Tomcat, because Tomcat instantiation is not part of the source code. In node.js it seems server is also part of the source code.
For now, the packaging is not my concern. My concern is how to separate the logic and how do I point all my servers to one source code base?
Node.js or JavaScript for that matter doesn't have a concept like WAR. But what it does have is something similar. To achieve something WAR like, you would essentially bundle the code into one source file using something like webpack. However, this will probably not work with Node.js modules like http (Express uses `http since it likely calls or relies on native V8/C++ functions/libraries.
You could also use Docker and think of the Docker containers as WARs.
Here is what I figured out as a work around:
Keep the servers under a folder say, "server_clusters" and put different node servers there, namely: node1.js, node2.js, node3.js, node4.js, etc (I know, in the real world, the clusters would be different VMs or CPUs altogether but for now, I simply want to separate server creation logic from source code). These files would have this code snippet:
var constants = require('./prop');
var appBasePath = constants.APP_BASE_DIR;
var appFilePath = appBasePath + "/main";
var app = require(appFilePath);
//each server would have just different port number while everything else would remain constant
app.listen(8080, function (req, res) {
console.log("server started up");
});
Create a properties file that would have the path to the source code and export the object. That simple. This is what is used on line#1 in the above code
Create the source directory project wherever you want on the machine and just update its home directory in the constant file above. The source code directory can export one landing file that will provide the express app to the servers to start:
var express = require('express');
var app = express();
module.exports = app;
With this, there are multiple servers that are pointing to the same source code.
Hope this helps to those who are facing the same problem.
Other approaches are welcome.

Trying deploy nodejs

I'm noob on nodejs and i'm trying some tutorials of nodejs. I'm trying this tutorial: http://cestfait.ch/content/chat-webapp-nodejs it works wonderful on my localhost but not when I upload to appfog like you can see here: http://nodebruno.hp.af.cm/
For example, the prompt don't show up. I changed the code to avoid the prompt and insert the nickname on a input text and this work on localhost but doesn't work on appfog too.
I already tried on nodejitsu servers and I have the same problem
Can you help me ?
Your app has an error, it's trying to connect to a localhost socket.io server.
You need to change this line:
var socket = io.connect('http://localhost:8000');
to
var socket = io.connect();
And it would work preferably on Nodejitsu

Resources