DevMountain / Siri

A mini-project to practice basic Node.JS fundamentals and server-side programming
3 stars 237 forks source link

Siri

A mini project to practice basic Node.JS fundamentals and server-side programming.

Objectives

Use Node.JS to build a simple socket server that responds to commands with text.

Step 1: Check out the Web Client

Navigate to http://devmountain.github.io/Siri-client/. This is a really simple Angular app that needs its back end completed. So far you can send messages, but there are no replies coming back. We're going to build the reply part.

Step 2: Create server.js

Before we start coding we need to run through some basic app setup. First of all, in your command line inside your project directory run an npm init command. This will create a package.json file for you and allow you to save npm dependencies. Before installing anything, we'll want to add a gitignore, so we're not committing massive amounts of dependencies to your repo.

echo "node_modules/" > .gitignore

It would be a good idea to add .DS_Store (and any other platform-specific binaries and potential security risks) to your gitignore as well.

Now we can run npm install express --save.

Create a server.js file in your repo and begin next steps:

Step 3: The GET call

The Siri-client is looking for a connection on port 8887 and will try to send a GET request to get a message.

var messages = ["Hello there.", "I'm sorry, I cannot take any requests at this time.", "I can tell you how to do that."];
app.get('/', function(req, res) {
  res.send(JSON.stringify({
    message: getRandomMessage()
  }))
});

Step 4: Cleaning Up

If your Postman request is working, great! You'll notice that the Siri client isn't yet working. This is because browsers are very careful about data they get from other domains; cross-domain requests are an easy exploit target. So we need to add in an extra call that the browser is making so it will allow data to come from our server.

app.options('/', function(req, res) {
  res.status(200).set({
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'OPTIONS, GET, POST',
    'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
    'X-XSS-Protection': '1; mode=block',
    'X-Frame-Options': 'SAMEORIGIN',
    'Content-Security-Policy': "default-src 'self' devmountain.github.io"
  }).send();
});

You will need to add the same headers to your app.get method as well. It should look something like this:

app.get('/', function(req, res) {
  res.status(200).set({
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'OPTIONS, GET, POST',
    'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
    'X-XSS-Protection': '1; mode=block',
    'X-Frame-Options': 'SAMEORIGIN',
    'Content-Security-Policy': "default-src 'self' devmountain.github.io"
  }).send(JSON.stringify({
    message: getRandomMessage()
  }));
});

Now your server will work properly with the Siri client.

Copyright

© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.