0

I have simple app that use express (node) as server and android as client,I want to communicate between android and express over http

i have this code in express

var express = require( 'express' );
var http = require( 'http' );

var app = express();
var server = http.createServer( app );

app.get('/', function(req, res){
    console.log( "hello word" );    
});

i type in browser(PC) mydomain.com:8080 to access express and it works fine. I have 2 Question

  1. how i can send value use following url and receive in express ?

  2. if my question number 1 success,how it work if I access from android?

thanks

ltvie
  • 931
  • 4
  • 19
  • 48

1 Answers1

0
  1. You could send values in the URL itself, as parameters, like mydomain.com:8080/some_value. To get this value in your express app, you have to tell your / route that something is comming:

    app.get('/:your_param', function (req, res) {
      var json = JSON.stringify(req.params); // you get your_param here as a JSON object
      console.log("hello world: " + json);
      res.send(json); // you have to send a response, otherwise, your browser will wait forever...
    });
    

    You can sens value in a POST request too, but this post would became too long. Take a look at the express docs. Other thing that I should point out is since you're using express, you don't need the http module. And you have to make your app listen for requests, you must add the line app.listen(3000);. Remember that you can choose whatever port you want. In the express docs they show a very simple example:

    var express = require('express');
    var app = express();
    
    app.get('/', function(req, res){
      res.send('hello world');
    });
    
    app.listen(3000);
    
  2. To access you express app from your Android device, just use the browser, like in your computer.

Rodrigo Medeiros
  • 7,814
  • 4
  • 43
  • 54
  • thanks, i found that i want in this link http://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-node-js – ltvie Jun 05 '14 at 00:46