I have a problem with getting data from client side in NodeJS.
On the client side I've prepared JSON data as you can see in codepen
On the server side I'm trying to get these data from the client:
var express = require('express');
var mysql = require('mysql');
var app = express();
app.use('/', express.static('../client/app'));
app.use('/bower_components', express.static('../client/bower_components/'));
var server = require('http').createServer(app);
var bodyParser = require('body-parser');
app.jsonParser = bodyParser.json();
app.urlencodedParser = bodyParser.urlencoded({ extended: true });
//mysql connection setup
var connection = mysql.createConnection({
host : "localhost",
port: "3306",
user : "root",
password : "",
database : "db",
multipleStatements: true
});
app.get("/cities", function(req, res) {
console.log(res.body); //I'm getting nothing
var data= {
city: req.body.city,
country: req.body.country
};
var query = connection.query('INSERT INTO table SET ?', data, function(err, res) {
if (err) {
console.log(err);
} else {
console.log('success');
}
});
console.log(res);
});
server.listen(3000, function () {
'use strict';
});
What I'm doing wrong? Is there a way to debug NodeJS in ST? Thanks.
database : "",no db selected.