Code Monkey home page Code Monkey logo

siri's Introduction

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 Create a server.js file in your repo. Now next steps:

  • Require the 'http' module (Remember, you require a module and capture it in a variable.)
  • Create a server that listens on port 8887 (remember?).

##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.

  • Create an array of messages that Siri might say, for example:
var messages = ["Hello there." "I'm sorry, I cannot take any requests at this time." "I can tell you how to do that."];
  • When a GET request comes in, have your server reply back with a random message from that array. Send it back in an object, like so:
{message: 'hello'}

Remember, you examine the request object in your server callback function and check to see which method was used. Then you can send a response.

And don't forget, to make sure this is valid JSON, let's use the built-in JSON.stringify method to convert our object to JSON:

res.end(JSON.stringify({message: myMessage});

To test yourself, use Postman to create a GET request to your server. Make sure it returns

##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. It's an easy place for an attack. So we need to add in an extra call that the browser is making so it will allow data to come from our server.

  • If the request's method is OPTIONS (this is the call the browser makes), return the following header/response:
res.writeHead(200, {
  'Connection': 'close',
  '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'
});
res.end();

Now your server will work properly with the Siri client.

siri's People

Contributors

cahlan avatar skylersb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.