test the app we’re going to construct:
we can be built-in follow integrated tech stacks
we will built-in integrated grow built integrated our server app, run integrated built-instruction integrated:
mkdir duneserver
cd duneserver
vagrant init ubuntu/trusty64
We need our server to run Ubuntu 16.04, have a static IP cope with 192.168.50.4 and feature NodeJS established, so update the content material of Vagrantfile with integrated:
Vagrant.configure(“2”) do |config|
config.vm.box = “ubuntu/trusty64”
config.vm.provision :shell, path: “bootstrap.sh”
config.vm.network “private_network”, ip: “192.168.50.4”
end
Now, create a provision built-ing script named bootstrap.sh with the subsequently integrated content:
#!/usr/bin/env bash
apt-get update
curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
bash nodesource_setup.sh
sudo apt-get install nodejs -y
sudo apt-get install build-essential -y
npm install forever -g
the built-inbuilt integrated script above, we first update the apt repository and then we installation NodeJS from a non-public PPA, deploy build-important which is required via some NodeJS modules and install for all time which facilitates us hold a node script runnbuilt integrated for all time. Learn more Angular Training
Now, we start the vagrant server with the command:
vagrant up
allow’s create a package.json file with the content under built-in our dune server list built integrated:
{
“name”: “duneserver”,
“version”: “1.0.0”,
“description”: “a server monitoring app using Node & Angular”,
“main”: “index.js”,
“scripts”: {
“start”: “forever start index.js”,
“dev”: “nodemon index.js”,
“test”: “echo “Error: no test specified” && exit 1”
},
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“dependencies”: {
“epoch-charting”: “^0.8.4”,
“nodemon”: “~1.11.0”,
“os-monitor”: “~1.0.5”,
“socket.io”: “~2.0.3”
}
}
Our bundle.json built-incorporates integrated 3 dependencies:
os-screen–a very simple display for the 7fd5144c552f19a3546408d3b9cfb251 os module built-in Node.js,
node mon–display for any changes to your integrated node.js utility and rout built integrated restart the server – best for improvement,
socket.io–enables real-time bidirectional event-primarily based verbal exchange.
allow us to create our built-index.js file with the subsequently integrated content:
var io = require(‘socket.io’)(8000);
var osm = require(“os-monitor”);
io.on(‘connect’, function (socket) {
socket.emit(‘connected’, {
status: ‘connected’,
type: osm.os.type(),
cpus: osm.os.cpus(),
});
});
io.on(‘disconnect’, function (socket) {
socket.emit(‘disconnected’);
});
osm.start({
delay: 3000 // interval in ms between monitor cycles
, stream: false // set true to enable the monitor as a Readable Stream
, immediate: false // set true to execute a monitor cycle at start()
}).pipe(process.stdout);
// define handler that will always fire every cycle
osm.on(‘monitor’, function (monitorEvent) {
io.emit(‘os-update’, monitorEvent);
});
built-in the code above we imported socket.io and created a server integrated on port 8000, next we imported os-screen.
The socket.io server will emit a jobuiltintegrated event while a consumer app connects with it, so on built-integrated, we ship the os built-in (osm.os.type) and range of CPUs(osm.os.cpus()) to the consumer.
integrated last sections of the code above, we built-integrated monitoring the server integrated osm.start(), at a built-interval integrated of 300ms. os-monitor emits a display event on every display cycle, so at the display, we use socket.io to ship the monitor event to the client.
allow’s ssh integrated the vagrant server, install the node modules and start integrated the webserver:
vagrant ssh
cd /vagrant # vagrant synced directory
npm install
npm run dev
constructing the Frontend
we can be the usage of Angular IDE by means of CodeMix, so in case you do not have it hooked up move beforehand and download it from here.
permit’s get began through firing up our Angular IDE and create a brand new Angular challenge named DuneServerMonitor.
as soon as the assignment is created we can now add Socket.io and Chart.js to our project.
npm install socket.io chart.js --save
allow us to include bootstrap CSS in our project, including the subsequent traces to the top segment of our src/index.html file.
<!doctype html>
Let’s understand the code snippet above:
First We used the Component decorator to mark our class as an Angular component and provide additional metadata that determines how the component should behave. The app.component.ts uses the content of app.component.html as it’s the template and applies the style rules in app.component.css.
Then Our app class implements the OnInit interface which we imported from @angular/core, so we can use the ngOnInit life cycle hook called by Angular to indicate that Angular is done creating the component.
Finally, we created some class variables to be used in the succeeding sections:
the socket which is an instance of the socket.io client
memChart currently undefined, but will be assigned as an instance of Chart from Chart.js
cpuChart currently undefined, but will be assigned as an instance of Chart from Chart.js
cpuType and noOfCpu will be used to store the variables.
Next, we created a Doughnut chart to be used to display free memory and, we created a line chart to be used to display the 15-minute CPU load average.
In section 2 first, we empty the array that holds the labels for the free memory chart whose initial value is [‘Free’,‘Used’], then we push two new labels of the format Free: XXMB and Used: XXMB, then we empty the dataset for the doughnut chart and push new values, then we update the chart.
If you have questions about any of the steps, please do ask also in the comments section below. Also, follow as Nodejs Certification
#nodejs #angularjs #angular #app #node-js