PostgreSQL connection via javascript - javascript

I am searching for a way to connect to postgresql directly in the browser. Im trying to utilize nodejs and browserify but have had no luck so far with the bundling. Whenever I compile a script that contains a
require('pg')
it specifically states in the browser:
Cannot find module '/node_modules/pg/lib/client'
the browser tells me afterwards that he is not able to find the modules that pg requires. Maybe i need to bundle pg with browserify before ?
I appreciate if somebody has an idea on how to work on this or even a suggestion how I could connect via javascript to pgsql.

While some node modules may be generally reusable to some degree in a web browser, most take advantage of Node.JS specific features or drivers and cannot work in any web browser. A case like a Postgresql package is a perfect example, as it requires many functions simply not present in a web browser.
If you look at the APIs of Node.JS (http://nodejs.org/api/), these APIs are not available in the browser (some could be emulated, but many are file system, low level sockets, binary modules, etc.).
If you want to use Postgresql, you'll need to build a web server layer, and expose an API of your own (likely a RESTful style api) and call the web services to perform the database actions you want to use. You could look at using Connect or Express to make writing the web service layer more convenient.

Related

Can I create web api using express.js and not have node.js installed?

I am currently in the process of creating a portfolio website for myself but due to hosting restrictions, I cannot make use of Node.js.
I know Angular can run on any web server, but is it possible to make use of Express.js to create web api's with relying on Node.js to run these web api's using Express.js?
If not, is there an alternative solution to create web api's that I can call using Angular and later for my mobile version of my website?
Please note that my shared hosting runs using cPanel.
As per definition Express.js, or simply Express, is a web application framework for Node.js so you can't do that. Alternatives would be to use a different backend language.
That also depends if your server supports them, for example, you can go with .NET CORE
You cannot use Express without NodeJS by definition so you have to deploy your backend somewhere else in you want to use it.
I suggest giving a look Firebase: you could write your backend using http cloud functions in express without paying anything until a reasonable amount of traffic (after that, is pretty cheap). You could also get rid of cPanel and deploy your frontend there via Firebase hosting.
Maybe you can try to build at first a web application with express. Of course you can create a web app without express if you need it. With express and Node.js I created a MySQL REST API. With HTML and Ajax you can fetch the Data from the API. So you can create two applications. One application where you need to run Node.js because it`s much easier to create a REST API with express. The second one is fully without Node.js.
Maybe there are better solutions, but inside each Web Application you can than but you can then access this API in any web application using jQuery. It doesn't matter if it is written with PHP, ASP.Net Core, Java EE / EE4J. You can also access this API in Ruby, Angular, React, Vue etc. using an AJAX request.
In some scenarios you can't start Node.js as a server because an application is already running on apache2 or nginx. There this would be a workaround to use something like this. For example, one could also integrate applications with HTML+JS in a CMS system that accesses other database tables and thus extend such a system without an iframe.
So can be helpful for few scenarios. Now just doesn't get around the actual goal of doing without Node.js completely or even express. But why are there REST APIs? So that you can query the data and incorporate it somewhere else. Otherwise you would have to build a REST API with another technology. Especially in the example of accessing MySQL with JavaScript, this would not be quickly feasible.
If you are looking for a similar solution to separate the web app and the REST API, but you don't need Node.js, then you should really build a REST API with .Net Core or with another technology, depending on what is possible and installed on your server. It could be Java or PHP behind it or Ruby.
The API that provides the REST access does not have to be written in JavaScript. You only need to be able to access it with JavaScript. So you can use many different approaches to access JSON data. I hope that in the short time with my bad English I have explained the basic idea, how to proceed stylistically and where advantages exist in REST interfaces.
With this, it should be self-explanatory that you don't have to use NodeJS and Express, but with JavaScript it's a pleasant solution. Only you have to ask yourself if a JavaScript application has to provide this interface at all or if in the end only a JavaScript application has to access this interface. Very big difference.
For backend rest api you can use golang with gorilla framework. Golang simple keyword and easy to learn.best important point is performance. If your server support golang you can use golang for backend..
ExpressJS is NodeJS framework so it's impossible to create an API without NodeJS.
Angular is front-end framework so you can host it on web hosting server.
If you need to create back-end APIs, you can use other clouding host servers that support NodeJS.
It's fairly simple to build this with just the net/http package. Set up a router that handles various commands and deal with the response accordingly.

Sails.JS or Loopback for Electron App

I am developing a image capture/storage software for school photographers that uses an angular front-end using electron to make it a native cross-platform desktop app. The app will need to have online and offline access. I will run a database on the client machine while offline and when online access is obtained it will sync to a cloud based database. In the future I want to be able to have the option for enterprise customers to run the application on premise and link to their own databases as well.
I was looking into using either Sails.js or Loopback to do this. Do you think one of the frameworks would be better for my particular use case? I would assume that both frameworks would be able to sync the offline data from multiple clients to the master cloud database using transactions easily? Any input you have would be appreciated! Thanks
(Also would react and redux be a better option for the font-end with electron opposed to using angular?)
Deciding which one suits you best is your call, but technically speaking Loopback can do what you need.
The offline/online sync is referred as isomorphic Loopback. Basically, you can run loopback client-side in offline mode, and when you get a connection it will sync with the remote server (that, ultimately, decides if the local data is accepted or not, depending on access control, validation, etc). There is an example repository. Be aware that this functionnality is still considered experimental.
Transactions are supported by some database connectors but not all of them. You can find the documentation here.

How to depoly a Node.js app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have installed npm locally and used it to install Bower, Grunt, Polymer & Yeoman. But I am still a little confused as to what Node.js actually is.
Back in the day I used to setup an Apache server locally, install phpMyAdmin and get to work on my project. I could then take the SQL database & the entire file directory for my project that included php, html, css etc. files and upload to my server and it would run.
I have noticed that PHP is not the way to go anymore, and that everyone seams to be running JS frameworks like Angular, Backbone & Ember, also Polymer looks interesting but I am not sure if it's a framework.
So my question is how do I deploy something built with node.js? e.g. a dashboard website/app built in Angular, Backbone, Ember or Polymer.
I am also guessing that SQL would not be the way to go anymore, but a noSQL option would be, such as PouchDB or Mongo? I really don't know how people like to store data with these new frameworks, maybe they still use SQL?
Can I simply copy the process of PHP & SQL and upload the SQL database & then the file dir and it will run on the server?
I have used firebase to deploy a polymer website/app, but I would like to know if I could use a standard web host instead of firebase or heroku.
There are 2 common ways to go about this. You can think of them in terms of client app and server app.
Server
If you're just creating a Node.js server, you may want to use the library Express (which is a wrapper around Node.js's http) in order to initialize a server. Then when you upload your source code to, say, AWS EC2 or Heroku (just as an example), you tell your startup script to invoke your server (if you server initialization code is in ./server.js, for example, you'd tell your EC2 or Heroku script to run node server.js).
Client app
There are a lot of tools to compile client apps (static sites), many of which work with Node.js: for example, Bower and Webpack. In this case, Node.js is only responsible for running this build tools and compiling your app for you. Then, it's as simply as copying the compiled static files into your hosting provider (like AWS S3, for example). Outside of the scope of the answer, but you can use continuous integration tools like Codeship or CircleCI in order to compile and upload those static files to your hosting provider automatically.
Hybrid
If you want Heroku to serve a static site, you can set it up in a way where Node.js is actually running a server that serves static files. This is a combination of the 2 approaches above because you need to actually be running an http server inside of Node.js that is responsible for serving your static content. You can also have an API server embedded in the same place, but a common practice is to have a separate API server somewhere else (separation of concerns).
A couple additional comments:
SQL would not be the way to go anymore, but a noSQL option would be, such as PouchDB or Mongo?
False. SQL and NoSQL both have their benefits. This is out of scope, but on a high-level, there are differences between schemas (whether maintained on the database server or the client server), operations likes joins, etc. (which plays into scalability), and the features of each individual database provider: for example, Redshift is a SQL database based on Postgres that uses columnar storage which is good for running, say, sum operations across gigantic datasets. There are Node.js drivers for it, you just have to pick your poison.
I would like to know if I could use a standard web host instead of firebase or heroku.
I wouldn't refer to a host using the term "standard", as essentially all hosting providers do the same thing: serve content. If you're using Node.js, you have many choices for hosting providers: AWS S3, AWS EC2, Heroku, Bluehost, and myriad other providers.
I don't think you can simply copy the files unless you write a script that will automatically call npm init (or whatever your startup scripts are) when the files have changed.
As long as you have shell access on your server you can probably use Apache or nginx as a reverse proxy for your node app, and set up a push-to-deploy system with git fairly easily.

How to browserify require('net').Socket?

I am trying to connect and query my MS SQL DB from Javascript in a browser (i.e. Chrome, NOT IE do not want to use ActiveX controls). In order to do that I found this Node library Tedious and Browserify.
Tedious provides API to connect and query MS SQL server. Browserify would walk over the dependency tree and generate a bundle.js with all dependencies.
So I tried to Browserify Tedious so that I could connect to my DB straight from my client side javascript running in the Chrome browser. The error I get is that the Socket object in the net package is undefined. Any idea why?
Edit 1
Thanks Peter for the response. Ok Browserify was a stretch.
Can I use websockets to connect and perform SQL queries in my MSSQL db? In other words, can an ODBC driver be written in JavaScript using WebSockets? I know this is going to be insecure. But this is meant for in-house only behind a firewall.
I don't believe it is possible to run tedious in a browser. Browserify is powerful, but not magical, despite the wizard mascot icon. Node.js provides filesystem, networking, and child_process APIs that are not available in the browser. In a few special cases, browserify magic can make certain things you expect to be node-only work in the browser, but generally a module will have to be pure JavaScript or JavaScript using official web browser APIs (DOM, AJAX, WebSockets, etc) to work in the browser.
This is a package that works well with browserify :
https://www.npmjs.com/package/net-browserify
It's based on a websocket proxy.

Node.js + Node-Webkit + Node-SerialPort Based Application - Is this Possible?

I am new to Node.js and before I go head first into taking on a new technology and migrating my c# based application, I wanted to make sure what I had in mind is possible with Node.js and if it is recommended.
So please let me know your opinion!
My application has the following requirements:-
TCP server (to receive packets from TCP clients such as smart phones, computers etc.)
Serial port access (To control a hardware device)
Web server facilities (to serve HTML5 pages or provide web services intefaces)
Simple native app like GUI for configuration.
All of this needs to be packaged nicely for end users to install simply.
Why I would want to do this you may ask?? The reason I am most interested in using Node.js is due to the cross platform nature, including the ability to install on cheap single board/embedded computers.
This is my thinking:-
Node.js - to provide the TCP server, serve HTML5 pages and provide web services interfaces.
Node-Webkit - to provide the simple native app like configuration interface. I also believe that it provides the ability to package my application for simple distribution, but I am not sure? (I could leave out Node-Webkit if there was another way to package my app for simple distribution. Although it is preferred as even having to find the IP Address of the computer so as to access a web browser interface would be tricky for some of my users.)
Node-SerialPort - to provide the ability to communicate with the hardware device. The instructions will come from 1 of three sources. 1) TCP connection 2) HTML5 Webpage Initiated 3) Web Services Initiated.
That's what most people use Node.js for
https://npmjs.org/search?q=serial+port
Again, that's what most people use Node.js for
Because of 1 and 3, the most obvious UI for node.js apps is the browser. Write a config page to control your app. Using node-webkit is overkill, just use whatever browser is already available. Node can run multiple listeners on multiple ports, serving the same app.
npm install is pretty simple.
Go learn how to write simple Node.js apps (with express for easy http servering, and jade or nunjucks for easy html templating) and then move up from there.
Point 5. I understand the fact of having an installer. I would suggest that the .msi or .exe will create a Windows Service for NodeJs. Then the node js server will launch at Windows startup and the end user will access the browser with
http://localhost:< a port number>/.
Packaging NodeJS : To package nodejs, you can copy the nodejs.exe from the installation directory of nodejs into the directory where you developp the node application. Then zip everything. You unzip it on another computer and it will execute.

Categories

Resources