# Please need help to start working with NodeJS

**URL:** <https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809>\
**Category:** Cloud\
**Created:** [June 26, 2015, 4:14pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809 "2015-06-26T16:14:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 4:14pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/1 "2015-06-26T16:14:16Z")

</div>

Hey Guys  
Please I need your help to start working on the NodeJS to create a server on my PC. This server will listen to an event from my spark core (As I believe by using GET), which published data in cloud. I’d like to know how can I write this code? Is these any sample code to start with?  
Thanks in advance.

[Woah there on the pings. Give people time to respond before pinging so many people OR just ping 1 or two at the start if it’s really specific - @harrisonhjones]

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 4:20pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/2 "2015-06-26T16:20:43Z")

</div>

Let’s start with this:

1. Install Node.JS
2. Install the [Javascript SDK](http://docs.particle.io/photon/javascript/)
3. Look at the [event stream](http://docs.particle.io/photon/javascript/#events-subscribing-to-events) documentation
4. Report back with findings

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 4:22pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/3 "2015-06-26T16:22:31Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 7:10pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/4 "2015-06-26T19:10:29Z")

</div>

Dear @harrisonhjones  
I did steps 1&2. For step 3, what I understood is I should change the event name from (test) to be my event name. Is this right? How I’ll specify my device?  
If I understood the idea of event stream, what is the next step? How can I start writing a code to read the event data, and save it to a text file on my PC?  
Thank you so much.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 7:17pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/5 "2015-06-26T19:17:24Z")

</div>

Steps:

1. Setup up a Particle Device to publish a message every 2 seconds or so. Ideally a JSON formatted message if you can
2. Try to use the event stream code provided in the docs to grab those messages (try `console.log(...)`ing them)
3. Post your code for 1. and 2. (don’t accidentally put your username, password, or access token when posting) in a [Gist](https://gist.github.com) and report back with the link here on the forums and we’ll help

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 8:17pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/6 "2015-06-26T20:17:47Z")

</div>

Hello @harrisonhjones

> [@harrisonhjones](#):
>
> Setup up a Particle Device to publish a message every 2 seconds or so. Ideally a JSON formatted message if you can

For step 1, I've already written a code in my Spark Core to publish an ascii data coming from sensors. Is this what you mean by this step?

> Try to use the event stream code provided in the docs to grab those messages (try console.log(...)ing them)

For Step 2, do you mean this code?

> //Get test event for specific device  
> spark.getEventStream('test', 'DEVICE\_ID', function(data) {  
> console.log("Event: " + data);  
> });

Where I should write this example? in Particle Dev, or in a new (.js) file?

Do I need to include these at the beginning of my code?

Thanks.

> park.listDevices(function(err, devices) {  
> var device = devices[0];

> console.log('Device name: ' + device.name);  
> console.log('- connected?: ' + device.connected);  
> console.log('- variables: ' + device.variables);  
> console.log('- functions: ' + device.functions);  
> console.log('- version: ' + device.version);  
> console.log('- requires upgrade?: ' + device.requiresUpgrade);  
> });You can ask for a specific device by it's id with: spark.getDevice  
> spark.getDevice('DEVICE\_ID', function(err, device) {  
> console.log('Device name: ' + device.name);  
> });

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 8:21pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/7 "2015-06-26T20:21:48Z")

</div>

So you’ve got two different sets of code: firmware (on your Spark Core) and software (on your computer, for now).

If you can go to [dashboard.particle.io](http://dashboard.particle.io) and see your events appear every 2 seconds or so you have the firmware figured out. If not, you’ll need to write some code in the WebIDE/Particle Dev

The software (node JS code) will go into a new .js file somewhere which you will run with the `node name-of-your-file.js` command.

Throw the entirety of your node.js file on a Gist (minus your username,password, or access token) and link to it here. I’ll help you out.

And yes, I do mean that part for the event stream

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 8:28pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/8 "2015-06-26T20:28:41Z")

</div>

Sure @harrisonhjones. I can see my event data in the dashboard each 2 or 3 seconds. Do I need to put my name.js file in the directory of the node folder? Or I can put it anywhere? The last thing that I want to know before start writing the file.js code is do I need to include something else other that the event stream and the following below:

> park.listDevices(function(err, devices) {  
> var device = devices[0];

> console.log('Device name: ' + device.name);  
> console.log('- connected?: ' + device.connected);  
> console.log('- variables: ' + device.variables);  
> console.log('- functions: ' + device.functions);  
> console.log('- version: ' + device.version);  
> console.log('- requires upgrade?: ' + device.requiresUpgrade);  
> });You can ask for a specific device by it's id with: spark.getDevice  
> spark.getDevice('DEVICE\_ID', function(err, device) {  
> console.log('Device name: ' + device.name);  
> });

Thanks a lot.  
Ahmed

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 8:30pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/9 "2015-06-26T20:30:35Z")

</div>

Take a look at this: [https://github.com/spark/sparkjs/blob/master/examples/node/get-event-stream.js](https://github.com/spark/sparkjs/blob/master/examples/node/get-event-stream.js)

It can go anywhere. You just need to open up a terminal / command prompt and run `node name-of-file.js`

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 8:53pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/10 "2015-06-26T20:53:20Z")

</div>

Hey @harrisonhjones

> <https://gist.github.com/ahmedsa1983/ef94c5a37ec982a57ab6>

I tried to run it in the NodeJS command line after I put my CoreID with the user name and password, but I got this error:

> C:\sparkJS\examples\node\>node sparkjs

> module.js:340  
> throw err;  
> ^  
> Error: Cannot find module 'spark'  
> at Function.Module.\_resolveFilename (module.js:338:15)  
> at Function.Module.\_load (module.js:280:25)  
> at Module.require (module.js:364:17)  
> at require (module.js:380:17)  
> at Object. (C:\sparkJS\examples\node\sparkjs.js:5:13)  
> at Module.\_compile (module.js:456:26)  
> at Object.Module.\_extensions..js (module.js:474:10)  
> at Module.load (module.js:356:32)  
> at Function.Module.\_load (module.js:312:12)  
> at Function.Module.runMain (module.js:497:10)

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [June 26, 2015, 8:59pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/11 "2015-06-26T20:59:38Z")

</div>

Hi @Ahmedsa1983

Have you seen the doc page for the Javascript SDK? This explains how to install it for node.

[http://docs.particle.io/core/javascript/](http://docs.particle.io/core/javascript/)

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 9:00pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/12 "2015-06-26T21:00:47Z")

</div>

ah. Did you download the entire library/SDK? If so, comment in `//var spark = require('../../lib/spark');` and comment out `var spark = require('spark');`

In the future, your project files should be somewhere else and then you can run `npm install spark` and it’ll work!

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 9:06pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/13 "2015-06-26T21:06:33Z")

</div>

Yes @bko  
I saw the documentation and installed the node.  
Thanks

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 9:26pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/14 "2015-06-26T21:26:02Z")

</div>

Hello @harrisonhjones  
Yes I downloaded the whole library as described in the [doc.partoicle.io](http://doc.partoicle.io).  
I did comment in //var spark = require('../../lib/spark'); and comment out var spark = require('spark');  
I still have the following errors:

> C:\SparkJS Project\>node sparkjs

> module.js:340  
> throw err;  
> ^  
> Error: Cannot find module '../../lib/spark'  
> at Function.Module.\_resolveFilename (module.js:338:15)  
> at Function.Module.\_load (module.js:280:25)  
> at Module.require (module.js:364:17)  
> at require (module.js:380:17)  
> at Object. (C:\SparkJS Project\sparkjs.js:4:13)  
> at Module.\_compile (module.js:456:26)  
> at Object.Module.\_extensions..js (module.js:474:10)  
> at Module.load (module.js:356:32)  
> at Function.Module.\_load (module.js:312:12)  
> at Function.Module.runMain (module.js:497:10)

For the npm install spark, I did it in a separate folder and it is installed successfully without errors.

Any Suggestion please?  
@bko @harrisonhjones

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 9:32pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/15 "2015-06-26T21:32:40Z")

</div>

Let’s try a “clean slate”.

1. Go to a brand new folder, open up a command prompt, and issue the following command: `npm install spark`
2. In that same folder copy, create a new file called “spark-test.js”, and paste your original (without the comment out thing) code (ie, [this](https://gist.github.com/ahmedsa1983/ef94c5a37ec982a57ab6))
3. Open up a command prompt and then issue this command `node spark-test.js`
4. Report back!

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 10:05pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/16 "2015-06-26T22:05:42Z")

</div>

Thanks @harrisonhjones for your clear explanation. I got the same data that I'm seeing on the dashboard, but I don't know how can I save just the event data on the text file? Right now, I'm receiving:

> Event: {"data":"SA0001FC00B00000000","ttl":"1","published\_at":"2015-06-26T22:04:  
> 07.803Z","coreid":"00000000000000000","name":"MyEvent"}

How can I just save the MyEvent data, which is SA0001FC00B00000000, to a text file?

Thanks.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 10:10pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/17 "2015-06-26T22:10:44Z")

</div>

Try adding `console.log("Data: " + data.data);` after `console.log("Event: " + JSON.stringify(data));` and see what you get. If you get the data you were expecting then look into adding something like this:

```auto
var fs = require('fs'); 
fs.appendFile('myAwesomeLog.txt', data.data, function (err) {
    console.log("There was an error writing the data to the log file");
});

```

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 10:14pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/18 "2015-06-26T22:14:21Z")

</div>

@harrisonhjones  
I got the following:

```auto
Event: {"data":"SA000FE000007B00000000C00400000E","ttl":"1","published_at":"2015
-06-26T22:12:55.937Z","coreid":"0000000000000000","name":"MyEvent"}
Data: SA000FE000007B00000000C00400000E

```

Is there another way to display only the data of the MyEvent?  
Thanks

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [June 26, 2015, 10:17pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/19 "2015-06-26T22:17:19Z")

</div>

Also when I tried to add these:

```auto
var fs = require('fs'); 
fs.appendFile('myAwesomeLog.txt', data.data, function (err) {
    console.log("There was an error writing the data to the log file");
});

```

I got the following errors:

> C:\Spark\>node spark-test.js

> C:\Spark\spark-test.js:7  
> fs.appendFile('myAwesomeLog.txt', data.data, function (err) {  
> ^  
> ReferenceError: data is not defined  
> at Object. (C:\Spark\spark-test.js:7:35)  
> at Module.\_compile (module.js:456:26)  
> at Object.Module.\_extensions..js (module.js:474:10)  
> at Module.load (module.js:356:32)  
> at Function.Module.\_load (module.js:312:12)  
> at Function.Module.runMain (module.js:497:10)  
> at startup (node.js:119:16)  
> at node.js:906:3

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 26, 2015, 10:23pm UTC](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809/20 "2015-06-26T22:23:56Z")

</div>

Your `fs.appendFile(...)` isn’t quite in the right place. I went ahead and [forked your code](https://gist.github.com/harrisonhjones/8dcc4788ec49f8dca0f1) and made edits. You’ll need to edit it to match your setup. Let me know how it goes

[Next page](https://community.particle.io/t/please-need-help-to-start-working-with-nodejs/12809.md?page=2)
