Language - JavaScript node.js: Difference between revisions

From Phidgets Support
(Created page with "{{NoTitle}} {| |style="vertical-align:middle; width: 60%;"| <font size=6>'''Language - JavaScript''' '''JavaScript with Node.js'''</font> Welcome to using Phidgets with Java...")
 
(Redirected page to Language - JavaScript)
 
Line 1: Line 1:
{{NoTitle}}
#REDIRECT [[Language - JavaScript]]
{|
|style="vertical-align:middle; width: 60%;"|
<font size=6>'''Language - JavaScript'''
 
'''JavaScript with Node.js'''</font>
 
Welcome to using Phidgets with JavaScript! By using JavaScript, you will have access to the complete Phidget22 API, including events.
 
Node.js is an open-source, cross-platform JavaScript run-time environment that allows programs written in JavaScript to be run locally.
|{{TOC limit|2}}
|}
 
==Install Phidget Drivers for Node.js==
Before getting started with the guides below, ensure you have the following components installed on your machine:
 
# You will need the [[Operating_System_Support|Phidgets Drivers]] installed on the server machine
# You will need the Node.js Phidgets library. Use <code>npm install phidget22</code>
 
=== Version Change ===
'''Note:''' The Phidgets JavaScript library has been bumped to version 2.x.x following a rewrite. The version 2 API is mostly identical to version 1, but does have some breaking changes. It is highly recommended that any code written against version 1 be updated to version 2, as version 1 is considered unstable.
 
== Phidget Network Server ==
The JavaScript library requires the [[Phidget Network Server]]. Start by configuring the server for your OS:
 
* [[OS - Windows#Phidget Network Server|Windows]]
* [[OS - OS X#Phidget Network Server|macOS]]
* [[OS - Linux#Phidget Network Server|Linux]]
* [[OS - Phidget SBC#Phidget Network Server|PhidgetSBC]]
 
The Phidget Server includes a built-in Webserver. This must be enabled when using the JavaScript library in browser, but can be left disabled when using the library from Node.js.
 
The Phidget Server Webserver can be used to serve files - such as the Phidget JavaScript library, or your own projects. By default, it serves the JavaScript control panel files. The main purpose of the Webserver is to support a Websockets connection for the Browser library - because regular sockets cannot be used in Browser. The Node.js library uses raw sockets to connect to the Phidget Server, and so does not require the Webserver or Websockets.
 
{{Finding Code Samples|JavaScript}}
 
==Using the Code Samples==
To use the code sample from the [{{server}}/?view=code_samples&lang=JavaScript Code Samples] page, you can click the '''Download Example''' button to download a Java file with the sample code.
 
[[Image:JavaScript_Sample_Code_Download.png|link=|center|border|800px]]
 
 
Next, open the command prompt at the file location of your example and enter the following command:
<syntaxhighlight lang='bash'>
npm update
</syntaxhighlight>
 
Then enter the following command to run the example (replacing example.js with your example name):
<syntaxhighlight lang='bash'>
node example.js
</syntaxhighlight>
 
Success! Your program is now running with Phidgets.
 
{{Language Page What's Next}}

Latest revision as of 17:01, 15 March 2021