To run Node.js

What is Node.js?

Node.js is a JavaScript application for running on the PC, not the browser. Software. You can’t use obniz unless you have that page open when you run it in a browser, whereas Node .js allows you to run it on your computer or server all the time, so you can use it every hour. You can easily do things like save the temperature to Dropbox. Furthermore, there are plenty of libraries built for Node.js, so it’s easy to create a variety of software. It is.

Features.

  • Because it runs on a PC or server, the program can run constantly.
  • You can do a lot of things with the Node.js library.

Install Nodejs

If you’re new to Nodejs, the first step is to install Nodejs!

https://nodejs.org/

You can download it from this site.

Create Nodejs Project

Let’s create a Node.js project.
The only thing you need to do is create a folder and put one or two files inside it. First, you need to create a folder, which can be any name. The name can be anything, and if you’re on Windows, you can use the command line. Then you create the folder from the command line on Windows, or from the shell on Mac or Linux. into

npm init

and command. You will be asked for various questions, but press Enter and proceed for now. Then there is a package.json in this empty folder.
This is the file of the project.

Next, we need to prepare app.js to write the app. Prepare an empty text file, name it app.js and save it in this folder . Your project is now complete!

Install obniz.js

Next, install obniz from npm.
The browser used to use "<script>" tags to load external programs, but Node.js uses a software called npm to install the software in name only.
npm install obniz 

to run the program. This will download the latest version of obniz.js and allow you to use it from within the program, and you can use I did.

Coding

Let’s open app.js in your editor and write the program. First of all, let’s import the “obniz.js” program that we just installed. Load the software you installed in Similar to the way it was loaded in the browser with .<script>

const Obniz = require("obniz");

That’s OK. After this, it’s exactly the same as when I was programming in HTML. jQuery is no longer used. So be careful. First connect to obniz. The way they are described is the same.

var Obniz = require("obniz");

var obniz = new Obniz("1234-5678");
obniz.onconnect = async function () {

}

And you can write various programs in onconnect for the same purpose. It’s the same as HTML to run a servo motor.

var Obniz = require("obniz");

var obniz = new Obniz("1234-5678");
obniz.onconnect = async function () {
  var servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
  servo.angle(90);
}

You can do it this way.

Implementation

You can run this JavaScript from the command line or from a shell using

node app.js 

and command to run it. And this app will continue to run until you make it stop. To stop, press “Ctrl + C”.

Cooperation with other services

There are various packages available on npm.
The ability to integrate with other web services is the main difference between npm and HTML.

For example, if you’re on Twitter.
https://github.com/desmondmorris/node-twitter

Dropbox.
https://github.com/dropbox/dropbox-sdk-js

There are libraries such as
Let’s create various things by integrating these libraries with obniz.