This sample BrightAuthor application shows how to instantiate a node.js server that runs on a BrightSign player. It creates a web server that can serve static files from the root folder on port 9090.
This example was created for BrightAuthor.
- Download Nodejs, if you don’t have it already, to install Node.js and npm.
- Your computer and BrightSign player must be connected to the internet.
- BrightAuthor should already be downloaded and installed on your computer.
- Clone the project or download ZIP, and then unzip node.js-starter-project-master.zip.
- If you have not run npm previously, run npm init in your terminal to initialize the install. If you wish, you can import other modules using npm install for additional functionality.
- Create an html file or use the node-server.html file that is provided in the node.js-starter-project-master folder, as the application entry point.
- Import the node modules you will need in your application to function and write your JavaScript code (in this example, the node modules are all included in express and the JavaScript is in index.js). From the Downloads folder, run:
npm install --save express
- Run these Webpack commands to package your application (see Appendix A: About Webpack for more details):
npm install --save-dev webpack webpack-cli copy-webpack-plugin zip-webpack-plugin
npx webpack --mode production
- Create a plugin, or use node-server.brs from the example, to launch your Node.js application.
- In the BrightAuthor Presentation Properties under Autorun, add the script plugin to enable Node.js in your presentation.
- Under Files, add the dist/node-server.zip file from your source directory.
If you have used the unmodified example, enter the http://<ip_address_of_ brightsign>:9090/filename in a browser (replace filename and the ip address with your specific information). It should then display the readme.md file.
The node_modules directory contains a lot of unnecessary files in addition to the code your application needs, and its size can be a problem when you deploy your application to an SD card or publish it to the cloud. Webpack, which bundles JavaScript modules to use in browsers, can solve this problem.
If you have a node.js application that has an index.html that calls an entry point function called main():
<body onload='main()'>
If this function is inline in the html, you will want to move it to a new file, index.js. This file would contain the references to your other JavaScript files, and to the node modules:
var myutils = require('./utilities'); // My utilities.js
var moment = require('moment'); // A node module
function main() {
myutils.foo();
moment.now();
...
}
Your index.html would then need to reference index.js:
<script src='./index.js'></script>
To use webpack, attach main() to the window object so that it can be found from index.html (the actual definition gets uglified):
var myutils = require('./utilities'); // My utilities.js
var moment = require('moment'); // A node module
function main() {
myutils.foo();
moment.now();
...
}
window.main = main;
Change index.html to point to the bundled JavaScript, and to window.main():
<script src='./bundle.js'></script>
<body onload="window.main()">
- An example is included to help you use BrightSign Javascript objects. See the "externals" section of the webpack.config.js.
- See the BrightSign Node.js documentation for more information.