Node.js beginner- request without query strings - javascript

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.use(express.bodyParser());
app.options('/', function (request, response)
{
response.header("Access-Control-Allow-Origin", "*");
response.end();
});
app.get('/', function (request, response)
{
connection.query("SELECT * FROM pathfind;", function (error, rows, fields) {
for(i=0;i<rows.length;i++)
{
response.send('<div id="layers">'+rows[i].layers+'</div> \
<div id="speed">'+rows[i].speed+'</div> \
<div id="direction">'+rows[i].direction+'</div>');
}
response.end();
});
});
app.post('/', 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?

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();
console.log(postData);
});
}

Related

How to post an array to Express API

I'm creating APIs with Express.js and SQL Server. I been posting an object which is easy and very simple, but now i have a new question: how to post an array?
Let's say i have a table which stores only two params:
Table_A
Id | CouponId
In fact, the only record that stores is CouponId, 'cause the Id is created by SQL Server on every record. So, the case is that i get a list of coupons from an api, and the idea is select from one to 'n' with a checkbox and save the selection.
This my code so far:
function getList(){
$http.get('/api/coupons')
.then(function(data){
$scope.infoCoupons = data.data.Response;
}
On the HTML view:
<div class="col-lg-12" align="center">
<input type="checkbox" ng-click="selectAll()"/> <a style="font-size:17px;color:black;text-decoration:none;">Select all coupons</a>
<ul class="coupon-list">
<li ng-repeat="coupon in infoCoupons">
<input type="checkbox" ng-model="coupon.Select" ng-click="checked"/> <a style="font-size:17px;color:black;text-decoration:none;">{{coupon.CodeCoupon}}</a>
</li>
</ul>
</div>
Then, to get the selected coupons:
$scope.selectAll = function(){
$scope.all = !$scope.all;
$scope.infoCoupons.forEach(function(o){
o.Select = $scope.all;
});
}
function chosenCoupons(){
var result = new Array();
var checked = 0;
$scope.infoCoupons.forEach(function(e){
if(e.Select === true){
result.push(e.Id);
checked +=1;
}
});
if($scope.all || checked > 0){
alert("Selected coupons!");
}
else if(checked === 0){
alert("Select at least one coupon");
}
}
Then, my code for the API:
const express = require('express');
const bodyParser = require('body-parser');
const sql = require('mssql');
const app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
const dbConfig = {
user: "daUser",
password: "daPass",
server: "daServa",
database: "daDB"
};
const executeQuery = function (res, query, parameters) {
sql.connect(dbConfig, function (err) {
if (err) {
console.log(`There's an error: ${err}`);
res.send(err);
sql.close();
}
else {
var request = new sql.Request();
if (parameters && parameters.length > 0) {
parameters.forEach(function (p) {
request.input(p.name, p.sqltype, p.value);
});
}
request.query(query, function (err, result) {
if (err) {
console.log(`Theres an error: ${err}`);
res.send(err);
sql.close();
}
else {
res.send(result);
sql.close();
}
});
}
});
}
app.post("/api/testApi", function(req, res){
parameters = [
{ name: 'CouponId', sqltype: sql.VarChar, value: req.body.CouponId }
];
var query = "INSERT INTO [Table_A] VALUES(#CouponId)";
executeQuery(res, query, parameters);
});
const PORT = process.env.PORT || 8080
app.listen(PORT, () => {
console.log(`App running on port ${PORT}`)
});
This is the code that usually works for an object. My question is: how can i send result (where result is the obtained array) on the API. I need to change something on my code on parameters?
Hope you can help me. I'm using Javascript, Node, Express and SQL Server.
How to post an array to Express API
In angular you simply do $http.post(url, data).
If you assign data to an object that has an array, it will reach express.
Then express can parse the body since you have app.use(express.bodyParser()); so the object should be available to you on the body.
URL Query Params
If you are trying to use query parameters, to pass an array, lets say on attribute items so you simply declare it multiple times
items=1&items=2&items=3 should be parsed to req.query.items === [1,2,3]

Node.js API returns JSON in terminal but not in browser

Having a strange problem. Been searching for answers but nothing turns up. I'm doing a node api tutorial and it returns JSON from my mongoDB database in my terminal when I perform any GET request but in my browser or postman I get nothing back, only in the terminal do I get any response. When I try a POST in postman it says it can't connect to the backend.
here is my code :
var http = require('http');
var url = require('url');
var database = require('./database');
// Generic find methods (GET)
function findAllResources(resourceName, req, res) {
database.find('OrderBase', resourceName, {}, function (err, resources) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resources));
});
};
var findResourceById = function (resourceName, id, req, res) {
database.find('OrderBase', resourceName, {'_id': id}, function (err, resource) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resource));
});
};
// Product methods
var findAllProducts = function (req, res) {
findAllResources('Products', req, res);
};
var findProductById = function (id, req, res) {
findResourceById('Products', id, req, res);
};
// Generic insert/update methods (POST, PUT)
var insertResource = function (resourceName, resource, req, res) {
database.insert('OrderBase', resourceName, resource, function (err, resource) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resource));
});
};
// Product methods
var insertProduct = function (product, req, res) {
insertResource('OrderBase', 'Product', product, function (err, result) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(result));
});
};
var server = http.createServer(function (req, res) {
// Break down the incoming URL into its components
var parsedURL = url.parse(req.url, true);
// determine a response based on the URL
switch (parsedURL.pathname) {
case '/api/products':
if (req.method === 'GET') {
// Find and return the product with the given id
if (parsedURL.query.id) {
findProductById(id, req, res)
}
// There is no id specified, return all products
else {
findAllProducts(req, res);
}
}
else if (req.method === 'POST') {
//Extract the data stored in the POST body
var body = '';
req.on('data', function (dataChunk) {
body += dataChunk;
});
req.on('end', function () {
// Done pulling data from the POST body.
// Turn it into JSON and proceed to store it in the database.
var postJSON = JSON.parse(body);
insertProduct(postJSON, req, res);
});
}
break;
default:
res.end('You shall not pass!');
}
});
server.listen(8080);
console.log('Up and running, ready for action!');
You have several callbacks with err as first argument but you are not treating any potential error. It means if something is going wrong, you are not catching it and returning an error. I don't know if this has anything to do with it, but as a practice (not even "best", but general practice) instead of doing this
function (err, resource) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resource));
}
do this
function (err, resource) {
if(err){
// do something to warn the client and stop here
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resource));
}
Try that, see if you are actually running into errors before trying to output an answer.
https://nodejs.org/api/http.html#http_response_end_data_encoding_callback
The response end method not send data to response socket. Maybe you change it
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resource));
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(resource));
res.end();
if you want socket to close to do something, you can into callback to end.
res.end(#logHandle());
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var connect = function (databaseName, callback) {
var url = 'mongodb://localhost:27017/' + databaseName;
MongoClient.connect(url, function (error, database) {
assert.equal(null, error);
console.log("Successfully connected to MongoDB instance!");
callback(database);
})
};
exports.find = function (databaseName, collectioName, query, callback) {
connect(databaseName, function (database) {
var collection = database.collection(collectioName);
collection.find(query).toArray(
function (err, documents) {
assert.equal(err, null);
console.log('MongoDB returned the following documents:');
console.dir(JSON.parse(JSON.stringify(documents)));
//console.dir(documents);
callback(null, documents);
}
)
database.close();
})
};
I think we are going through the same tutorial, this is my solution of 'database.js', works for me.

Node JS OR Express JS HTTP GET Request

I am using express.js and I need to make a call to HTTP GET request ,to fetch JSON data .Please suggest me some good node js/express js modules/lib to perform get/post request .
Node.js provides an extremely simple API for this functionality in the form of http.request.
var http = require('http');
//The url we want is: 'www.random.com/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.com',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
http.request(options, callback).end();
Here I attach some more examples with POST and custom headers. If you don't need special things, I'd stick to the native code.
Besides, Request, Superagent or Requestify are pretty good libraries to use.
var express = require('express');
var app = express();
var fs = require('fs');
app.get('/', function (req, res) {
fs.readFile('./test.json', 'utf8', function (err, data) {
if (err) {
res.send({error: err});
}
res.send(data);
})
});
var server = app.listen(3001, function () {
console.log('Example app listening port 3001');
});

Read from request method in Node.js

I'm writing a Node.js script that converts HTML files to ENML (Evernote Markup Language).
Now this script correctly converts an existing HTML file to the desired ENML output. Now, I have the following question:
Client will be sending an HTML file in JSON format. How do I listen for all incoming requests, take the JSON object, convert to ENML, and write back the response to the original request?
My code for this is as follows:
var fs = require('fs');
var path = require('path');
var html = require('enmlOfHtml');
var contents = '';
var contents1 = '';
fs.readFile(__dirname + '/index.html', 'utf8', function(err, html1){
html.ENMLOfHTML(html1, function(err, ENML){ //using Enml-js npm
contents1=ENML;
});
});
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(contents1);
}).listen(4567, "127.0.0.1");
Thanks!
I guess that the client will make POST requests to your server. Here is how you could get the send information:
var processRequest = function(req, callback) {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
callback(qs.parse(body));
});
}
var http = require('http');
http.createServer(function (req, res) {
processRequest(req, function(clientData) {
html.ENMLOfHTML(clientData, function(err, ENML){ //using Enml-js npm
contents1 = ENML;
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(contents1));
});
});
}).listen(4567, "127.0.0.1");
You can use the Node's request module.
request('http://www.example.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});

Node js : receive data from client

I have a problem. Send data from the client to the server, to the database. But the server does not see the data from the client, if you do this:
var http = require("http");
var url = require("url");
var Db = require ("mongodb").Db;
var Server = require("mongodb").Server;
function start () {
'use strict';
function onRequest (request, response) {
'use strict';
var db = new Db ("TestApp", new Server ("127.0.0.1", 27017, {}));
response.writeHead(200, {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"});
db.open (function (err, db) {
db.collection ('ObjectCollection', function (err, collection) {
var url_string = request.url;
var params = url.parse(url_string, true).query;
console.log(params["name"]);
collection.find().toArray (function (err, docs) {
console.log (docs);
response.write(JSON.stringify(docs));
response.end();
});
});
});
}
http.createServer(onRequest).listen(8080);
console.log ('Server has started...')
}
exports.start = start;
And this is how he sees, but since I can not add data to the database
var http = require("http");
var url = require("url");
var Db = require ("mongodb").Db;
var Server = require("mongodb").Server;
function start () {
'use strict';
function onRequest (request, response) {
'use strict';
var db = new Db ("TestApp", new Server ("127.0.0.1", 27017, {}));
response.writeHead(200, {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"});
db.open (function (err, db) {
db.collection ('ObjectCollection', function (err, collection) {
collection.find().toArray (function (err, docs) {
console.log (docs);
response.write(JSON.stringify(docs));
var url_string = request.url;
var params = url.parse(url_string, true).query;
console.log(params["name"]);
response.end();
});
});
});
}
http.createServer(onRequest).listen(8080);
console.log ('Server has started...')
}
exports.start = start;
Help me, please :)
Not sure what you are trying to do, but I will do my best to give you advice.
Your code for parsing the query parameters looks fine. A GET request to http://localhost:8080/?name=foo
will result in params having values, with property 'name' = 'foo'.
If you are trying to insert a document with a property 'name', e.g. {name: 'foo', property2: 'param2', etc...} into your mongoDB collection, you will want to implement a mongoDB insert into your collection, NOT a find.
If you want to do a find, but query by {name: 'foo'}, you will need to pass that as a parameter to find().
I suggest you take a look at the mongoDB driver documentation for more on find and insert: http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html
If you just need mock data to return, you can alternatively open up the mongo shell in the command line, $mongo, use the TestApp database use TestApp, and do a db.ObjectCollection.insert({...your document here...}) to populate your database with data, so your server's query can return some documents. MongoShell commands are also documented on the MongoDB website.
Hope it helps.

Categories

Resources