How to interact with Node JS from client side - javascript

I am building a web portal to interact with internal databases and potentially run ssh commands. I chose NodeJS with Express for this purpose.
Light weight server-side interactions such as templating or routing I managed with Node and Express but I am looking for ways to do heavy tasks such as DB work (insert/update/delete) or run node code with the click of a button from client-side.
My research brought a couple of ideas however I want to use the one which is future-proof, secure (but not vulnerable to security configurations) and scaleable. Please share your thoughts and guidance on the matter?
Socket IO (may not be scaleable)
REST API
AJAX (Has issues with AJAX and CORS in the past)
Other approaches????
Thanks

For what you are trying to accomplish, you should be looking into the MEAN stack. I would recommend that you use Angular4, Loopback (Built on top of Express), MySQL, and of course Node. It is very easy to make an angular sdk that will allow you to use the same loopback models that your server side will be using. As a result you will be able to do your heavy tasks such as DB work :)

Related

Node.js / Express - how to think about client-side database work? (e.g. can't use require())

I've been through a number of Node.js, Express, and other tutorials/posts, and I'm struggling with how to think about connecting to a database on various pages throughout a webapp.
I would like to run a Node.js app (with a server.js file that connects to a database) and then query that database as needed on every page throughout the app.
So if I have an inventory.html page I should be able to have javascript that queries the inventory table and displays various inventory items throughout that html page.
Problem #1. I can't find a way to use mysql on any client-side pages, since javascript can't use node's require() function client-side. As detailed in this StackOverflow post ("require is not defined").
Problem #2. I can't figure out an elegant way to pass a database connection to other pages in my app. A page can send a POST request back to the server.js file, but this really isn't as flexible as I want.
I'm really looking for the modern, preferred way to do a bunch of PHP scripting in my Node app. Can anyone guide me to the right way to do this? Thank you!
You just can't directly call mysql from the client. Even if it worked imagine that anybody could modify the SQL queries and access all your data.
The only way how to do it is this:
js client app ------> js server app -------> mysql
You just must have 2 apps: one running in the user's browser sending requests to the server and the other running on the server answering the requests.

How to implement a node.js backend server with express.js and ember-cli?

I've googled for few days, some tutorials talking about using ember-cli to build an ember app, but most of them are teaching me to separate the server into two.
That is, one for providing the API endpoint to query the database (with custom express server, mongoDB...), and one for hosting the website (with ember-cli), it means that I have to start two node.js backend servers to serve one website.
Can I do it in one node.js app?
Yes, the only limit to how many "sites" you can run in a single node.js app is based on the hardware of your server. Even the smallest of servers should be able to handle running your api + the website endpoints in one node.js app.
However, what you have isn't two node.js apps, you have an express.js api, and then an ember-cli webapp. It isn't built in such a way that it would be easy to simply consume your ember-cli webapp with your api or the other way around, so i'd have to go with "Yes, it is probably possible", however, good luck making it happen.
I would avoid trying to combine them.

How to set up a JS Require tool to require('net') on the client side?

So my application is running Sails.js, which is an extension of node.js. I'm very new to the JS backend framework scene, and I'm attempting to send a TCP message using a socket from the client side. However, in order to do that I need to require('net'). How can I do this? I don't even see the net module in my node_modules folder - does it not come with Sails.js?
How can I resolve this issue?
Also, just for extra clarification, I need to use require() on the browser side - apologies for not being more clear in my original question.
You can't use the node.js net module on the client side. Browsers don't allow you to access plain TCP sockets. That would enable you to circumvent many of their security features, so it simply is not going to happen.
You can implement realtime communication with your own server with web sockets or a wrapper suck as socket.io, but that obviously doesn't let you talk in arbitrary protocols.

Starting a couchdb DB in a HTML + Javascript web

I have a website built with HTML and JavaScript, served by Tomcat.
I need to add a no-SQL database to it, and I'm trying Couch DB. I'm in trouble, 'cause I understand how the DB works, but I have no idea how to connect it with my website.
I'm used to SQL, where I just have to make a connection and then send SQL queries. How can I instantiate a connection object to CouchDB, and use couchdb.js? Currently, Tomcat answers me with a cross domain issue, 'cause Tomcat and couchdb are on different ports.
Can anybody help me with the very basics?
Cross-origin resource sharing (CORS) will be implemented only in the next release of CouchDB (v.1.3).
It could be surprising but one should realize that it is not the "very basics" of CouchDB:
Most of CouchApp web apps developers serve their Ajax applications from CouchDB itself (using HTML shows, lists, and attached static pages).
Some developers use CouchDB as a simple database within a 3-tier architecture.
Others send requests to CouchDB from desktop or signed applications (Java, Flash, etc.).
So, sending Ajax queries from code retrieved from external sites was relatively rare.
My recommended advice would be to adopt one of the most common settings.
If it is definitely not suited to your case, then you can either test CouchDB's development version, or use a proxy so that CouchDB appears to be on the same server as your HTML code (until the next release).
First, you need to get familiar with curl, then you start to test your couchdb through command line.
Trying on the same machine that you installed couchdb.
$ curl localhost:5984
After that, try to access from a different machine using curl:
$ curl http://<your_couchdb_server_name_or_ip>:5984/
If you can't, you need to check if your server has a firewall that are blocking outside requests to your server on 5984 port.
Now, to access your couchdb from a ajax request you must configure the local.ini file:
[httpd]
enable_cors:true;
[cors]
credentials:true;
mehods:PUT,GET,POST,DELETE,OPTIONS;
origins:*;
Restart your couchdb and try again, this must fix you problem.

ASP.NET MVC - Automatic notifications when a record is modified?

I have just used the NotifyIcon class in a windows application and I think it is really handy. I'm predominantly a web developer so I just wanted to find out if there is anything similar to this for a website.
The website I want to incorporate this into has a Ticket Management module where users can capture tickets/problems and then get responses to these tickets from my client's employees who handle the ticket.
Obviously I realize that the notification or pop up will need to be shown in the page, but is there a way to put a timer on the specific page, or even the Master page (maybe javascript or JQuery), to poll the database every few minutes and check for recently modified tickets and let the logged in user know that a ticket has been updated?
Thanks in advance.
If you need the client to keep an open connection to the server and poll it, I think Signal R will be your best bet for integrating into an .NET project. It is on Nuget but source is at https://github.com/SignalR/SignalR.
I would recommend familiarizing yourself with Node.js.
Node.js is a strong tool that aids in leveraging javascript as a real-time server management tool.
After you've gotten yourself familiarized with the Node.js setup, you'll want to grab Socket.io. Socket.io provides suppport for the long-polling technique by leveraging against your websocket created by Node.js. Here, we can manage the conditions at which we serve data. This is a huge tool on the developers side in battling against 2 HTTPD ports.

Categories