Why does my restify JSON client not work? - node.js

I'm following the example here
http://mcavage.github.com/node-restify/#JsonClient
As follows
var client = restify.createJsonClient({
url: 'http://api.stackoverflow.com'
});
client.get('/1.1/users/19068', function(err, req, res, obj) {
assert.ifError(err);
console.log('%j', obj);
});
I'm getting the following error message
SyntaxError: Unexpected token
at Object.parse (native)
at /.../node_modules/restify/lib/clients/json_client.js:54:20
at IncomingMessage.<anonymous> (/.../node_modules/restify/lib/clients/string_client.js:149:16)
at IncomingMessage.g (events.js:156:14)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1147:24)
at TCP.onread (net.js:354:27)

Related

Node express POST Json document invalid json

I want to send a json document to a POST link route, on the backend, my API parses the json body with the bodyparser and inserts the document into a MongoDB Collection, however I always get "invalid json". When I am using w-xxx-urlencode and pass the fields with urlencode, it works normally. The json document should be valid itself:
{"_id": "5afa7fcb01b8c6e5a83aa8f6", "Timestamp":"2020-01-01T09:01:55.128Z","PB":9191,"pot":0,"nP":4343,"L":3333,"Lze":4444,"p":5555,"ne":66666666,"Pr":7777}
I get the error:
Error: invalid json
at error (C:\Users\U500405\git\Backend_node.js-Server\node_modules\body-parser\index.js:97:13)
at C:\Users\U500405\git\Backend_node.js-Server\node_modules\body-parser\index.js:50:63
at IncomingMessage.onEnd (C:\Users\U500405\git\Backend_node.js-Server\node_modules\raw-body\index.js:119:7)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
When I am leaving out the first "_id" field,I get the error:
POST /link/err 400 2.672 ms - 724
SyntaxError: Unexpected token T in JSON at position 1
at JSON.parse (<anonymous>)
at C:\Users\U500405\git\Backend_node.js-Server\node_modules\body-parser\index.js:52:25
at IncomingMessage.onEnd (C:\Users\U500405\git\Backend_node.js-Server\node_modules\raw-body\index.js:119:7)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
in the app.js module, I initialised to use Json bodyparser:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
My function to create the parsed json data and save in MongoDB is:
exports.create_an_FA= function(req, res) {
console.log(req.body)
var fa = new FA(req.body);
//SAVE the new instance
fa.save(function(err) {
if (err) {
console.log(err);
res.status(400);
res.send(err);
}
else {
console.log("Instanz FA in Datenbank erzeugt!");
res.status(200);
res.json({ message: 'FA-Instance created in datbase!' });
}
});
};
Why do I get the error no valid JSON data? it should switch automatically between json bodyparser x-www-urlencode and json(), depending on the body format right?
Edit:
I used the curl command to send post data as following:
curl -d "{\"Timestamp\":"\2020-01-01T09:01:55.128Z\","\PrognostizierterBetriebswert\":9191,"\posFlexPot\":0,"\negFlexPot\":4343,"\Leistungsuntergrenze\":3333,"\Leistungsobergrenze\":4444,"\posGesEnergie\":5555,"\negGesEnergie\":66666666,"\Preissignal\":7777}" -H "Content-Type: application/json" -X POST http://localhost:5000/DNZ/FA
and I get:
curl: (6) Could not resolve host: application
I tried without the escaping "\" character but then I get undefined or an empty document returned...

Can't set headers after they are sent when trying to redirect after using Google's oauth2Client.getToken()

After receiving oauth2 tokens from google I am trying to redirect to an endpoint on my express server to make api requests.
This is the flow of the auth requests:
app.get('/api/presentation/getpresentation/:id', function(req, res, next){
req.session.user = req.user.id;
req.session.presentation_id = req.params.id;
res.redirect(google_auth_url)
});
app.get('/oauth2callback', function(req, res) {
const code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
req.session.tokens = tokens;
if (!err) {
oauth2Client.setCredentials(tokens);
res.redirect(`/api/presentation/${req.session.presentation_id}`);
return;
}
res.status(500).send(err);
})
});
app.get('/api/presentation/:id', **do things with api consent**);
The error:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:767:10)
at ServerResponse.location (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:884:15)
at ServerResponse.redirect (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:922:18)
at /Users/joe/school/solo-project/present/server/index.js:112:11
at /Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/auth/oauth2client.js:154:5
at Request._callback (/Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/transporters.js:106:7)
at Request.self.callback (/Users/joe/school/solo-project/present/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1057:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Any help is greatly appreciated, I've been tearing my hair out on this one for a while!
Thanks!

Node.js 'socket hang up' and keep-alive

I have a very strange error happening.
I am running Node.js version 8.0.0 on Mac OS X Sierra.
I create a very simple http server that basically returns 'ok' to any request after 8 seconds in this example.
I then use the 'request' package to make a request to my http server every 9 seconds, and use the parameter 'forever' to keep the http session alive.
One request out of two works perfectly fine while the other out of two returns a 'socket hang up' error.
Here is the very simple code to reproduce the issue:
const http = require('http');
const request = require('request');
const port = 3000;
const server = http.createServer((req, res) => {
console.log('request');
setTimeout(() => {
res.end('ok');
}, 8000);
});
server.listen(port);
setInterval(() => {
request(`http://localhost:${port}/`, {forever: true}, (error, response, body) => {
if (error) return console.log('error', error);
console.log('done', body);
});
}, 9000);
When I run this code I get the following output on my Macbook Pro :
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
So I get an 'socket hang up' error exactly every other request.
It seems that the error does not seem to appear on an Ubuntu VPS though.
Any ideas as to why this issue is happening ?

Stripe Connection Error

I have the follow backend Node.js code to connect to my Stripe account to make a Charge. But I'm getting this error. I'm using Firebase Functions, and this functionality used to work before, so I doubt this is anything to do with Firebase access restrictions. Any ideas and help on this will be greatly appreciated!
var functions = require('firebase-functions');
var stripe = require('stripe')('sk_test');
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
exports.stripePay = functions.https.onRequest((request, response) => {
if (request.method === 'POST') {
var app = express();
var router = express.Router();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
var stripetoken = request.body.cardToken;
var amountpayable = request.body.amount;
var charge = stripe.charges.create({
amount: amountpayable,
currency: 'usd',
source: stripetoken,
description: 'Sample transaction'
}, function (err, charge) {
console.log("ST4");
if (err) {
response.send("Failed!");
}
else {
response.send({ success: true });
}
})
}
{ Error: An error occurred with our connection to Stripe
at Error._Error (/user_code/node_modules/stripe/lib/Error.js:12:17)
at Error.Constructor (/user_code/node_modules/stripe/lib/utils.js:98:13)
at Error.Constructor (/user_code/node_modules/stripe/lib/utils.js:98:13)
at ClientRequest.<anonymous> (/user_code/node_modules/stripe/lib/StripeResource.js:192:9)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at TLSSocket.socketErrorListener (_http_client.js:310:9)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at connectErrorNT (net.js:1020:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9) type: 'StripeConnectionError', stack: 'Error: An error occurred with our connection to Stripe\n at Error._Error (/user_code/node_modules/stripe/lib/Error.js:12:17)\n at Error.Constructor (/user_code/node_modules/stripe/lib/utils.js:98:13)\n at Error.Constructor (/user_code/node_modules/stripe/lib/utils.js:98:13)\n at ClientRequest.<anonymous> (/user_code/node_modules/stripe/lib/StripeResource.js:192:9)\n at emitOne (events.js:96:13)\n at ClientRequest.emit (events.js:188:7)\n at TLSSocket.socketErrorListener (_http_client.js:310:9)\n at emitOne (events.js:96:13)\n at TLSSocket.emit (events.js:188:7)\n at connectErrorNT (net.js:1020:8)\n at _combinedTickCallback (internal/process/next_tick.js:74:11)\n at process._tickDomainCallback (internal/process/next_tick.js:122:9)', rawType: undefined, code: undefined, param: undefined, message: 'An error occurred with our connection to Stripe', detail: { Error: getaddrinfo ENOTFOUND api.stripe.com api.stripe.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'api.stripe.com',
host: 'api.stripe.com',
port: '443' }, raw: { message: 'An error occurred with our connection to Stripe',
detail:
{ Error: getaddrinfo ENOTFOUND api.stripe.com api.stripe.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'api.stripe.com',
host: 'api.stripe.com',
port: '443' } }, requestId: undefined, statusCode: undefined } Reply
this is a good one! Firebase blocks external API connections for Free accounts, just get any paid account and this will work!
Dimitris answer is correct, in more layman's terms simply get on the blaze plan and you can make calls to stripe api from Firebase Functions.

Node.js csv-parse error mystery using Sails

It processes most of the records from the .csv file. However, at the end I get this error with csv-parse node module and can't figure out why. It terminates my server so I need to resolve it. Please help
events.js:160
throw er; // Unhandled 'error' event
^
Error: Number of columns on line 37052 does not match header
at Error (native)
at Parser.__push (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:222:13)
at Parser._flush (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:189:12)
at Parser.<anonymous> (_stream_transform.js:118:12)
at Parser.g (events.js:291:16)
at emitNone (events.js:86:13)
at Parser.emit (events.js:185:7)
at prefinish (_stream_writable.js:504:12)
at finishMaybe (_stream_writable.js:512:7)
at endWritable (_stream_writable.js:524:3)
at Parser.Writable.end (_stream_writable.js:489:5)
at ReadStream.onend (_stream_readable.js:511:10)
at ReadStream.g (events.js:291:16)
at emitNone (events.js:91:20)
at ReadStream.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
My Controller API
upload: function(req, res){
req.file('statData').upload(function(error, files){
var fs = require('fs');
var parse = require('csv-parse');
var csvData=[];
var lines = 0;
var filePath = sails.config.appPath + '/data/file.csv';
fs.createReadStream(filePath)
.pipe(parse({
columns: false,
delimiter: '\t',
skip_empty_lines: true,
relax: true
}))
.on('data', function(csvrow) {
csvData.push(csvrow);
})
.on('end',function() {
//do something wiht csvData
});
}
I switched to using fast-csv module and worked.

Resources