Rocco M Rocco M - 4 months ago 35
MySQL Question

MYSQL + Node.JS Post Request Confusion

I am very new to networking and I have this code which, when I use a REST API like Postman, does exactly what I want it to do:'/', function(req,res,next){
var reqObj = req.body;
req.getConnection(function(err, conn){
console.error('SQL Connection error: ', err);
return next(err);
var query = conn.query("INSERT INTO coordinates (id,lat,lon) VALUES(3,2,1);");
console.error('SQL error: ', err);
return next(err);
res.json("Coordinates sent.");
} );

That is, it sends the query request to the MYSQL database. My question is, how do I do this without using Postman to send the POST request?

Thank you.


You can't unless you make a post request from within your application or something. If you don't intend on sending data, you can just make it a GET request by changing'/', function(req,res,next){


router.get('/', function(req,res,next){

Then you can just go to the relevant URL from your browser. If you're using chrome and you just wanna see the JSON data, I'd also recommend installing the JSONView chrome extension.


Here's the example request using request-promise

var request = require('request-promise');
var objectData = {
    name: 'Bruce',
    alias: 'Batman'
var options = {
    method: 'POST',
    uri: 'http://your.api/endpoint/',
    body: objectData,
    json: true // Automatically stringifies the body to JSON

    // handle success response
}, function(error){
    // handle error response