jeudi 16 juin 2016

500 Internal Server Error on nodeJS expressJS project

I am writing a project on Android where my server side is running on NodeJS using expressJS framework. I am getting 500 Internal Server Error from my Server whenever, my android try to connect to the server however, I can reach the same server using the same URL.

Here is my android code to connect:

String link = "http://192.168.0.56:3030/registration";
//TODO change link 10.128.72.189
String data = URLEncoder.encode("mobile ", "UTF-8") + "=" +
        URLEncoder.encode(mobile_number, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email_address, "UTF-8");

data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password_enc, "UTF-8");

data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(keyValuePair, "UTF-8");

data += "&" + URLEncoder.encode("salt", "UTF-8") + "=" + URLEncoder.encode(salt, "UTF-8");

URL url = new URL(link);
Log.d("Error",link);
Log.d("Error",data);
URLConnection conn = url.openConnection();

conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

wr.write(data);
wr.flush();

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

StringBuilder sb = new StringBuilder();
String line = null;
String value = "";
// Read Server Response
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return new String("Exception");
}

The values are all good and okay... On the server side:

router.post('/', function(req,res)
{
  var email = req.body.email;
  var password = req.body.password;
  var mobileNumber = req.body.mobile;
  var key = req.body.key;
  var salt = req.body.salt;
  var uuidv4 = uuid.v4();
  console.log("post");

  post_response = res;
  if(email.length == 0 || password.length == 0 ||
   mobileNumber.length == 0 || key.length == 0
  || salt.length == 0)
  {
    required_missing_info(email);
  }
  else {
    console.log("validate email");
    req.checkBody("email").isEmail()
    var errors = req.validationErrors();

    if(errors)
    {
      invalid_email(email);
    }
    else {
      //email okay
      //step 3
      //check if email exist before
       console.log("email");
       connectionEnable();
       con.query('SELECT email_address FROM account_data Where email_address = ?', email, function(err, rows)
  {
  if(err)
    {
      //connection error
      table_connection_error(email, "account_data "+err);
    }
    else {
      //step 4
      //check if the rows length != 0
      //if account does exits
      if(rows.length != 0)
      {
        duplicate_email(email);
      }
      else {
        insert_data(email,password,key, salt,mobileNumber,uuidv4);
        return;
      }
    }
    //end of step 4
  });

However, I am not even reaching to error part. I am getting Connection Error message (Custom message) with no error! SO I am not whether it is in NodeJS or android side.

Here is some other part of the nodeJS code var express = require('express');

var router = express.Router();
var mysql = require("mysql");
var bodyParser = require("body-parser");
var validator = require("express-validator");
var uuid = require('node-uuid');


router.use(bodyParser.urlencoded({extended: false}));
router.use(bodyParser.json());
router.use(validator());

var post_response;

var con = mysql.createConnection(
{
  host: "localhost",
  user: "root",
  password: "123456",
  database: "sos_db"
});

function connectionEnable()
{
  con.connect(function(err)
  {
   if(err)
   {
     db_connection_response();
   }
  });
  }

Here is the list of packages

{
    "name": "SOS_server",
    "version": "0.0.0",
    "private": true,
    "scripts": {
    "start": "node ./bin/www"
    },
    "dependencies": {
        "body-parser": "~1.13.2",
        "cookie-parser": "~1.3.5",
        "debug": "~2.2.0",
        "express": "~4.13.1",
        "jade": "~1.11.0",
        "morgan": "~1.6.1",
        "serve-favicon": "~2.3.0"
    }
}

This is my first project with NodeJS and Android. I am probably missing a lot of things. Please do point them out. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire