Reputation: 427
I'm new to Node.js and am unsure how to use it for my website as defined in the following example. Here's what I'd like to do:
I have a website http://example.com with a real-time stock dashboard located at http://example.com/stocks. I'd like to use Node to
1) populate a mysql database (located on my web server through Namecheap hosting) with stock data scraped every 5 minutes (say from Bloomberg)
2) pull the stock data from the mysql database and serve it to the page to be displayed through the dashboard on a client's end when they visit http://example.com/stocks.
I'm by no means asking for the code behind this, I just don't know where to start! Every tutorial online seems to use their machine localhost
as the only place to display and use their Node output. How can I use Node to achieve the tasks above through my web server associated with my domain and not my localhost
machine? Thanks for any advice!
Upvotes: 0
Views: 339
Reputation: 181
This is easier than you may think. I would recommend using the MEAN stack or a slight alternative (still considered MEAN stack), MySQL, Loopback, Angular4+ (current), and Node.js.
With Loopback you are able to create Models, in your case, we may have a Stock model. The Model is connected to a datasource, in your case it would be MySQL. Loopback will automatically create the basic CRUD methods (GET, CREATE, UPDATE,...) as well create custom remote methods for further data manipulation. We can also build a Loopback SDK for an Angular client. The SDK allows us to call methods from our server side. In addition, the real-time socket.io is also generated when you build the SDK.
From this point, it is just handling the JSON object, error handling, and displaying these values to the user. You will be able to see both your client at localhost:4200 and your server at localhost:3000 (also use the explorer, at localhost:3000/explorer). With a little bit of set up you can build this to be a powerful application. I have written about and I am now teaching many of these concepts. Give my site a look link where I have written out this exact set up as well as others that are related to your use case. Hope this help. Please let me know if you have any more specific questions and I will do my best to help.
Upvotes: 1
Reputation: 1010
All examples are using localhost, because they are being developed locally. In order to have your application on http://example.com, you will need to deploy your application to a host machine.
In order to develop something like this in NodeJS, there are few things you should consider using. I would suggest following steps
Implementation of scheduled job, job that will collect/scrap the data from the site (see node-schedule module for details)
Store collected data in database of choice (you have mentioned mysql)
Implement socket connection (for example socket.io) which will be used by a web app to connect to server and receive the data in real time
Implementation of UI with websocket connection to the server
Take deployment as a last step (step where you see your application running on http://example.com). You need to find appropriate host, install all relevant stuff used in development (nodejs, mysql, npm modules,...), and then you can start your application on new host.
Hope this helps a bit on where to start and how to develop/deploy this in NodeJS.
Upvotes: 1