Async function not supported in NodeJS app deployed to Heroku - node.js

I'm trying to implement asynchronous functionality in one of my routes in a NodeJS app. It works when I run it on localhost but when deployed to heroku, an error unexpected token ( is thrown in the first line of the following code:
router.post('/post', async(req,res) => {
const data = await getData();
//do stuff
})
I'm a little confused as to why this works in one environment and not the other. Are there heroku configurations I'm missing? Do I need to explicitly add support for ES2016/2017 in Heroku?

Heroku defaults to the latest Long Term Support version of Node (which, as of writing, is v6.11.1) if you don't set a specific version.
async/await has been available in Node since v7.6, so you'll need to explicitly specify at least that version in your package.json, as documented here: https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version

you can refer this link
Es6_hrku
You need to convert your ES6 to ES5 in order to make it happen on heroku.
Changes required in package.json.

Related

why is so differult between the app generated by fastify-cli and the example in the documentation?

where is the start.js in the project which is generated by fastify-cli ?
i think its big different between the getting start example and the app generated by fastify-cli?
should i write the start function like this in the project created by fastify-cli?
const start = async () => {
try {
await sequelize.sync({})
app.log.info('database sync correctly')
await app.listen(PORT, '0.0.0.0')
app.swagger()
} catch (err) {
app.log.error(err)
process.exit(1)
}
}
start()
there are just a app.js in the project generated by fastify-cli.what a different!
where is the start.js in the project which is generated by fastify-cli ?
There is not, it is replaced by the CLI utility fastify your-file.js in the package.json (like mocha, jest etc.. does to run tests)
Usually the starter file is always the same, so it has been integrated in the cli and you can use the args to set the PORT or to reload the server automatically when you edit one file.
i think its big different between the getting start example and the app generated by fastify-cli?
The docs teach all you need to know about the framework, the plugins and utility around it want to ease the developer experience.. like manage a mongodb-connectio: it is one line with the official plugin.
should i write the start function like this in the project created by fastify-cli?
If you use fastify my-file.js you don't need it.
After some experience you will understand when you need the fastify-cli or not.
I think the cli is useful in the most use cases and it suggests good ways to implement configurations loading and encapsulation.
You won't need it for special use cases that need to run some async operation before the server is created

fs.existsSync is not a function error when requiring grpc through create-react-app

We are trying to get gRPC to work with React (actually we were trying to get it to work with React-Native but gave up on that for now).
Using plain node.js things are pretty straight forward if you follow this example.
We started by using create-react-app but when we started the app, we got the following error:
existsSync is not a function
That was casued by this bit in pre-binding.js belonging to the node-pre-gyp package located in grpc:
var existsSync = require('fs').existsSync || require('path').existsSync;
My understanding is that something goes on with Webpack (or some other process run by create-react-app) that goes and returns and empty object instead of require('fs').
Any ideas of how to get this to work without having to give up on the wonders of create-react-app?
To test it out you can just follow these 2 easy steps:
create-react-app test-app
add import grpc from 'grpc'; in the App.js file
Basically, from what I understand now, you are not really supposed to use gRPC + Protobuf directly from any frontend but rather it is more common to use the grpc node package on node.js server-side code and then communicate with the browser-side code using Express.
The server-side code on node.js then communicates using grpc with the microservices.
We are testing out the use of Firebase Functions to communicate securely with the frontend and Firebase Functions communicate with the Go microservices using grpc.

Azure Functions crashing loading MongoDB in Node

I'm attempting to write an Azure Function, in Node, to connect into a MongoDB instance (Cosmos DB in this case).
However, as soon I run require("mongodb"), my function crashes, without throwing an error, or logging anything, with the HTTP response returning a 502 code.
My setup:
Creating a function app using all defaults through the Azure portal.
Creating a package.json with mongodb version 3.x.
Running npm install through the Kudu shell
Include the require statement in my code.
Make a request to the function
This doesn't throw an error in the code, and I see logging that's run before, but not after the require statement (which is making it pretty difficult to debug).
I've also tried following through this guide about running a mongo query from a function, and it fails in exactly the same way for me.
After putting some hooks into Node's module module, my attempts to debug this led to a line in one of mongo's dependencies that fails in a similar way when run in isolation (from saslprep), which seems to stem from running out of stack space.
However, this feels like its a pretty mainstream use for an Azure function, and I haven't seen any similar issues, so I'm inclined to suspect that its an issue with my setup, rather than the mongodb library, but I haven't been able to find a misconfiguration, as I haven't changed any defaults - right now, I'm stumped!
I don't have a full code example right now, as I'm away from my work computer, but the code is something like
const mongo = require('mongodb');
module.exports = function(context) {
context.res = {
body: 'Hello world'
};
context.done();
}
Without the require statement, the code runs fine, returning the response to the browser.
It turns out that this problem was caused by running out of stack space. After pushing a patch to the saslprep library (v1.0.1), this has now been fixed.
Im pretty sure that if you add to your require function the same as in Microsofts Cosmos DB guides for mongo the following it should work
var mongodb = require('mongodb').MongoClient;
you have it as:
const mongodb = require('mongodb');
Im curious to know if that makes a difference. After looking through Microsofts own docs nearly all of them are declared that way.
Here is the tutorial I found: MongoDB app using Node.js

How to use the pg node package in angular

Situation
Hi, I'm quite new to Angular, I've been doing some projects following tutorials, which then lead me to try to start my own project to practice my Postgres and newly acquired Angular "skills".
I am trying to do a webapp that connects to a postgres DB using the node pg module.
(I know sequelize is a thing and it seems to work better than pg but AFAIK sequelize doesn't let you run pure postgres commands through it) Please correct me if I am wrong about this
The problem
This is where I get stuck, I am trying to follow the instructions from the docs but it doesn't seem to work correctly.
I have tried:
const { Client } = require('pg');
import { Client } from 'pg';
Also tried importing it in the .angular-cli.json in the scripts array
All of these fail with errors similar to this
ERROR in ./node_modules/pg/lib/connection-parameters.js Module not found: Error: Can't resolve 'dns' in '[...]\node_modules\pg\lib'
ERROR in ./node_modules/pg/lib/native/client.js Module not found: Error: Can't resolve 'pg-native' in '[...]\node_modules\pg\lib\native'
But nothing seems to work properly. Am I doing this completely wrong?
Also, pretty dumb question. I believe angular does everything on the client side, this is a HUGE security risk for DB access in prod. If that is true, is there a way to write server-side .ts services? or are services server-side?
You could write your serverside code in node using compiled ts, but probably not with angular.

Google Cloud Functions, Node JS 8.9.x (LTS) and KOA library

How can I use Koa library, the express replacement, in Cloud Functions?
I know KOA use all great ES2017 and make more use of Async use of JavaScript.
or it might not be needed at all working with Cloud Functions because the Firebase system won't send multiple calls to the same Cloud Function until it ends the previous one?
it unclear to me.
it know demands Node 8.x and I know the NodeJs 8.9.x, has now LTS.
Reading from cloud functions doc:
Base Image Cloud Functions uses a Debian-based execution environment
and includes contents of the gcr.io/google-appengine/nodejs Docker
image, with the Node.js runtime installed in the version, specified
above:
FROM gcr.io/google-appengine/nodejs
RUN install_node v6.14.0
To see what is included in the image, you can check its GitHub
project, or pull and inspect the image itself. Updates to the language
runtime (Node.js) are generally done automatically (unless otherwise
notified), and include any changes in the definition of the base
image.
And I saw a pull request back in November 2017, adding Nodejs v8. Here's hoping it can finally land in Google Cloud Functions 🤞🏻
UPDATE: Google Cloud Functions now support Node.js 8 and even Python!
Referring to the release notes from Google... Cloud Functions Release Notes
Node version supported is still at v6, same for firebase. You need to wait awhile before they release it in v8. Am pretty sure they will move to v8 when v6 no longer supported, but hopefully earlier...
Use babel:
index.js:
----------=
'use strict';
require('#babel/register')
require('babel-polyfill')
const http = require('http')
const createApp = require('./app/app.js')
const handle = createApp().callback()
if (process.env.IS_LOCAL_DEPLOYMENT) {
// to use same code in local server
http.createServer(handle).listen(3000)
} else {
module.exports.http = (request, response) => {
handle(request, response)
};
}
app.js:
--------
'use strict';
const Koa = require('koa')
module.exports = () => {
const app = new Koa()
app.use(......)
return app
}
package.json
------------
"scripts": {
.
.
"start": "export IS_LOCAL_DEPLOYMENT=true && node index"
.
.
}
I just saw in Cloud Functions Console editor for one of my functions that Node 8 is now a runtime option. See screenshot:

Resources