无法获取和阅读GET请求

时间:2017-07-21 13:04:49

标签: mysql node.js express get

我使用Node.js时遇到问题我通过GET请求将数据发送到特殊脚本,该脚本应读取此数据并将其写入MySQL。以下是GET请求的示例(很长):

http://demo.com/?&bssid=16~6D~57~7F~D8~AD&mac=60~01~94~2B~A8~85&rssi=0&hotspots_list=~~0_74~85~2A~39~77~48_-85~~~~1_78~8C~54~33~D8~3C_-91~~~
~2_64~6E~EA~0E~B3~26_-84~~~~3_76~85~2A~39~77~49_-87~~~~4_2C~33~7A~16~91~3B_-92~~~~5_54~BE~F7~6B~EE~43_-92~~~~6_98~DE~D0~AB~B8~64_-91~~~~7_14~DD~A9~1E
~F6~F8_-93~~~~8_64~6E~EA~0C~9A~3E_-84~~~~9_E8~DE~27~50~2A~12_-88~~~~10_84~C9~B2~0B~BA~C2_-81~~~~11_00~0F~94~BE~FC~44_-87~~~~12_56~BE~F7~6B~EE~44_-86~
~~~13_00~0E~8F~85~5F~73_-53~~~~14_0C~54~A5~A6~C1~1F_-87~~~~15_16~6D~57~7F~D8~AD_-78~~~~16_0E~54~A5~A6~C1~10_-92~~~~0_EC~1A~59~17~CF~5F_-91~~~~1_74~85
~2A~39~77~48_-92~~~~2_16~6D~57~7F~D8~AD_-77~~~~3_0C~54~A5~A6~C1~1F_-92~~~~4_0E~54~A5~A6~C1~10_-83~~~~5_84~C9~B2~0B~BA~C2_-82~~~~6_14~DD~A9~1E~F6~F8_-
92~~~~7_64~6E~EA~0C~9A~3E_-85~~~~8_E8~DE~27~50~2A~12_-93~~~~9_00~22~6B~56~2B~83_-92~~~~10_00~0E~8F~85~5F~73_-58~~~~11_00~0F~94~DB~DE~64_-91~~~~12_16~
DD~A9~1E~F6~F9_-92~~ 

我做了很少的实验,我发现它在PHP中工作正常,我的代码(仅用于输出):

<?php
echo $_GET["bssid"]." ".$_GET["mac"]." ".$_GET["rssi"]." ".$_GET["hotspots_list"];
 ?>

但不在Node.js中!这是我的Node.js代码:

var express = require("express");
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'host',
  user     : 'user',
  password : 'pswd',
  database : 'db'
});
var bssid, mac, rssi, hotspots_list;
const port = 3000; 
const host = "0.0.0.0";

app.get('/', function (request, response) {
  //Writing data to vars
  bssid = request.query.bssid;
  mac = request.query.mac;
  rssi = request.query.rssi;
  hotspots_list = request.query.hotspots_list;
  //Sending request to MySQL server
  connection.query(`INSERT INTO locations (mac, bssid_current, rssi_currnet, hotspots_list) VALUES ('${mac}','${bssid}','${rssi}','${hotspots_list}')`);
  //Debugging line
  console.log(`BSSID: ${bssid}`);
  //Response
  response.statusCode = 200;
  response.setHeader('Content-Type', 'text/plain');
  return response.send('Done');
});

app.listen(port, host, () => {
  console.log(`Server running!`);
});

1 个答案:

答案 0 :(得分:1)

在您的代码中,您的路线为/。您正试图通过/server.js

访问它

您添加两种解决方案 app.get('/server.js', [...])或您从网址中删除server.js后访问它。