Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js beginner- request without query strings

Node.js beginner- request without query strings


I'm new to node.js. A project I'm working on requires that a large array (625 items) be stored in a MySQL database as a string and I'm using Node MySQL to accomplish that.

Right now I have it so that the row is updated when a request is made to "/(column name)?(value)". Unfortunately, it didn't take long to learn that passing 625 items as a string to node isn't the most efficient way of doing things.

Do you have any suggestions for alternate methods of passing a large array as a string besides querystrings?

var http = require('http'),
   mysql = require("mysql"),
   url = require("url"),
   express = require("express");

var connection = mysql.createConnection({
   user: "root",
   database: "ballot"


var app = express();

app.options('/', function (request, response)
    response.header("Access-Control-Allow-Origin", "*");

app.get('/', function (request, response)
    connection.query("SELECT * FROM pathfind;", function (error, rows, fields) {
'); } response.end(); }); });'/', function (request, response) { console.log(request.param('data', null)); var urlData = url.parse(request.url, true); if(urlData.pathname = '/layers') { col = "layers"; } else if(urlData.pathname = '/speed') { col = "speed"; } else if(urlData.pathname = '/direction') { col = "direction"; } req.addListener("data", function(data) { connection.query("UPDATE pathfind SET "+col+"="+data+" WHERE num=0", function (error, rows, fields) { if (error) { app.close(); } }); }); }); app.listen(8080);**

EDIT: So now I know I need to use posts. I've rewritten the above code using express.js. Most examples that I look at online use posts with HTML forms, which is fine, but my project needs to post data via an AJAX request. Can someone show me how that data is parsed in node.js?

Problem courtesy of: Harangue


You can extract post data like this.

if (req.method == 'POST') {
    var body = '';
    req.on('data', function (chunk) {
        body += chunk;
    req.on('end', function () {
        var postData = body.toString();
Solution courtesy of: vinayr


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js beginner- request without query strings


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription