node.js apn Failed to generate token - node.js

As title, i use node-apn package to send push notification to iOS devices.
In my local machine (MacOS), i can send notification to device successfully, but when i deploy my codes to a CentOS server, i can't apnProvider object.
My Codes is:
var apn = require('apn')
var path = require('path')
let options = {
token: {
key: path.resolve('/path/to/f8/keyfile'),
keyId: 'myKeyId',
teamId: "myTeamID"
},
production: false
};
var apnProvider = new apn.Provider(options);
I got this error, (i tried google about it but can't find a solution):
/root/buzzship/node.js/node_modules/apn/lib/credentials/token/prepare.js:45
throw new VError(err, "Failed to generate token");
^
VError: Failed to generate token: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
at prepareToken (/root/buzzship/node.js/node_modules/apn/lib/credentials/token/prepare.js:45:13)
at config (/root/buzzship/node.js/node_modules/apn/lib/config.js:42:31)
at new Client (/root/buzzship/node.js/node_modules/apn/lib/client.js:11:19)
at EventEmitter.Provider (/root/buzzship/node.js/node_modules/apn/lib/provider.js:12:19)
at Object. (/root/buzzship/node.js/index-api.js:128:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
Please tell me what i am wrong at, thank you.

Thank you for suggestions, i solved this issue by upgrade node to version latest 7.10.0.

Related

How to using eventSource on nodejs

I am new on nodejs
ı have a project for server sent events.
I am trying get datas with server sent events on console.
this is my code:
var source = new EventSource('https://sse.now.sh');
source.onmessage = function(e) {
console.log(e.data)
};
but when i try start project with node file.js :
ReferenceError: EventSource is not defined
at Object.<anonymous> (c:\Users\SYDNEY\Desktop\server-sent-events-demo\src\client\js\app.js:7:5)
at Module._compile (internal/modules/cjs/loader.js:953:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
its just simple but i couldnt do anyting. should ı download anything about that ? bcz this script working on jsbin(website)
I solved this problem with add top of codes
var EventSource = require('eventsource')
var source = new EventSource('https://sse.now.sh');
source.onmessage = function(e) {
console.log(e.data)
};

firebase-fuctions ,TypeError: Cannot read property 'onRequest' of undefined

updated firebase via npm i -g firebase-tools ,
used my firebase id ,
while deploying my function at firebase ( with firebase deploy ) this error results out
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'onRequest' of undefined
at Object.<anonymous> (C:\Users\umer\Desktop\learning chatbots\usingFulfilmentAsWebhook\functions\index.js:9:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:15:15
at Object.<anonymous> (C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:53:3)
here is the code
const functions = require('firebase-functions');
exports.webhook = functions.http.onRequest((request,response)=>{
console.log("request.body.result.parameters", request.body.result.parameters);
let params = request.body.result.parameters;
response.send({
speech: `${params.name} your hotel booking request for ${params.roomType} room is forwarded for ${params.persons} persons. We will contact you on ${params.email} soon`
});
});
try after putting "s" in http in below line
exports.webhook = functions.https.onRequest((request,response)=>{
...
}
Add this in your first line
const functions = require('firebase-functions');
Besides, make sure you have necessary packages installed.

nodemon - app crashed - waiting for file changes before starting

I just started learning Node.js. The idea of this small application is to use express and mongosse to store some users in a based cloud database (mongoDB via mlab).
I have two seperate files :
User.js (models/User.js)
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
email : string ,
pwd : string
});
server.js (dossier root)
var express = require('express')
var cors = require('cors')
var bparser = require('body-parser')
var mongoose = require('mongoose')
var User = require('./models/User.js')
var app = express()
app.use(cors())
app.use(bparser.json())
app.post('/register', (req,res) => {
userData = req.body;
var user = new User(userData);
user.save((err, result) => {
if(err) console.log('IL YA UNE ERREUR')
result.sendStatus(200);
})
})
mongoose.connect('mongodb://user:pwd#ds261755.mlab.com:61755/myapp', { useMongoClient: true } , (erreur) => {
if(!erreur)
console.log('Connexion etablie');
})
app.listen(3000)
When I execute : nodemon server.js, I get the error below:
D:\Bureau\MEAN\appBackend\models\User.js:5
email : string ,
^
ReferenceError: string is not defined
at Object.<anonymous> (D:\Bureau\MEAN\appBackend\models\User.js:5:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Bureau\MEAN\appBackend\server.js:6:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
[nodemon] app crashed - waiting for file changes before starting...
Do you have any idea about this error?
Mongoose expects you to specify types using the built-in constructor functions, which are named with capital letters, e.g. String, Number, Boolean, etc.
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
email : String ,
pwd : String
});
define
module.exports = mongoose.model('User', new mongoose.Schema({
email : 'string' ,
pwd : 'string'
})
});
There is no string variable in the code .
this message will be appeared always when there's any small mistake.
I also faced this when module.exports type as module.export,just missed 's' only.
I come across with such error and on my case it was syntax error in the node js file.check your code for error as nodemon listen to change and run if no error there.
This was the error that make app-crashed ....
console.log("this is author",cope["author"];
SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19) After i correct the error [nodemon] restarting due to changes... [nodemon]
starting node server.js
That is it. Check for error in your code the nodemon automatically start the server.That is why nodemon is best rather than using node filename.js cz every time you have to start whenever you change the code.
The common reason is that you're trying to access the database from an IP that isn't whitelisted.
For my case, I was working on the same project on another PC. So, i had to whitelist the PC IP.
To do this, login to your cloud base mongodb, navigate to that your particular db project, and click on network access, add IP address and click on current ip address.
Also, make sure on your terminal, you run mongod to start your mongodb.
That solve for me
[nodemon] app crashed - waiting for file changes before starting...
You can fix this error with adding "start": "nodemon server.js" entry to your package.json file.
Test with this command:
npm install --save express-load
Many times we closed the PC and left the project running, when we used it again and ran the project again, that error appeared. I always solved it, restarting the pc. Everything worked perfectly.

Oracle connectivity with NodeJS and ExpressJS

I am trying to connect oracle with NodeJS. Steps followed as below. Pre - requisites there is no client installed except NodeJS(0.10.25) and npm(1.3.24).
In my ExpressJS Project:
npm install db-oracle
There is no error found while installing.
In app.js
var oracle = require('db-oracle');
var connString = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxxx)))";
var connectData = { "tns": connString, "user": "XPPS_OWNER", "password": "Ex3ec" };
In one of the endpoint i coded like this,
app.get('/oracleendpoint',function(req,res){
connectData.connect(function(error) {
if (error) {
return console.log("CONNECTION ERROR: " + error);
}
this.query().select('*').from('Users').execute(function(error, rows) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log(rows.length + ' ROWS');
});
});
});
Expected output is to connect the DB.
FYI: DB Instance is running fine.
Query:
What is the DB Name?
It is showing an error which show below. what would be root cause for this?
module.js:340
throw err;
^
Error: Cannot find module './build/Release/oracle_bindings'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous>
(C:\xampp\htdocs\mytest\node_modules\db-oracle\db-oracle.js:18:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
Please let me know any missing or Installation required or any node modules required or code changes. I have Googled but in vain. Haven't found proper document for this error.
Oracle themselves have just released an Oracle driver for node.js: https://blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node
Looks promising.
In your example, you use db-oracle module, I would recommend to use node-oracle module instead, as it is more popular/better maintained.
To connect to Oracle DB from Node, you have to follow the instructions on the page mentioned above.

Node.js crashing when connecting to AWS S3

After migrating my application from IntoVPS to Digital Ocean, my application crashes when it tries to upload photos to AWS S3.
The differences between the servers are: IntoVPS was running Ubuntu 10.10, and Digital Ocean is running Debian; IntoVPS had node 0.8.x, and Digital Ocean has 0.10.26.
There are no firewalls on the new server that could be causing this issue (I've checked).
The crash errors are:
Error: read ECONNRESET
at errnoException (net.js:904:11)
at Pipe.onread (net.js:558:19)
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
Some of these might be related to forever trying to restart the process (I figured that's what spawn might mean). The one I'd focus on is the ECONNRESET one.
So, I'm running my process using forever, and I'm using the knox module to connect to S3.
After googling I found: https://github.com/LearnBoost/knox/issues/198
I tried adding res.resume() to the callback of putFile like it says in the, but nothing changes; I still get the ECONNRESET error.
I've spent an entire day (yesterday) trying to fix this issue, and I can't continue having my production application be broken, so I decided to try switching to an older version of Node in order to temporarily (but quickly) fix this issue. So, I installed the n module to try and get 0.8.26 as the node version installed. Unfortunately, n didn't work, and here's the issue I created on that: https://github.com/visionmedia/n/issues/170
EDIT:
After exiting my ssh session and opening a new one, n is working. However, switching to 0.8.26 version of node causes another errors:
/apps/Foobar/node_modules/dnode/node_modules/weak/node_modules/bindings/bindings.js:83
throw e
^
Error: Module version mismatch, refusing to load.
at Object.Module._extensions..node (module.js:485:11)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at bindings (/apps/Foobar/node_modules/dnode/node_modules/weak/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/apps/Foobar/node_modules/dnode/node_modules/weak/lib/weak.js:1:97)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/apps/Foobar/node_modules/dnode/index.js:5:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/apps/Foobar/controllers/sock.js:2:13)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at global.Controller (/apps/Foobar/globals.js:2:9)
at Object.<anonymous> (/apps/Foobar/controllers/generosity.js:209:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at global.Controller (/apps/Foobar/globals.js:2:9)
at Object.<anonymous> (/apps/Foobar/app.js:282:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:245:9)
As you can see I'm having the darnedest time trying to fix this issue with S3, and I'm to the point of crying (metaphorically). I can't keep wasting time on this issue, but I can't seem to solve it. It's almost like Node itself is broken.
Can anyone provide any insight on what's going on? Any help with this is appreciated.
Why isn't knox working anymore?
EDIT
Here's more details.
Code that errors:
// S3
var endpoint = "https://s3.amazonaws.com/"+global.config.aws.s3.bucket+"/";
var knox = require('knox');
var s3 = knox.createClient(global.config.aws.s3);
var photosPrefix = 'photos/';
var model = module.exports;
model.emitter = new events.EventEmitter;
// ::Photos
model.createPhoto = function(info, cb){
if (!info.file || path.resolve(info.file) === path.resolve(global.__tempdir))
return cb(new Error('missing info.file'));
var db = info.db;
var photoId;
if (typeof info.public === 'undefined')
info.public = false;
// Move file to a specific location
var basename = path.basename(info.file);
var destination = photosPrefix+basename+'/original';
yarn
(function(){
// Upload photo to AWS
s3.putFile(
info.file,
destination,
{
'Content-Type': 'image/png'
},
this());
})
(function(err, res){
if (err) return this.error(new Error(err));
// Remove temporary file
fs.unlink(info.file, this());
})
(function(err){
if (err) return this.error(new Error(err));
// Retrieve photo stream from AWS
s3.getFile(destination, this());
})
(function(err, res){
if (err) return this.error(new Error(err));
// Get the dimensions of the photo using the stream (res)
gm(res).size(this());
})
(function(err, size){
if (err) return this.error(new Error(err));
// Insert record into photos table (include the dimensions, width and height)
db.query("INSERT photos (resource, width, height, public) VALUES (?, ?, ?, ?)", [basename, size.width, size.height, info.public], this());
})
(function(err, results){
if (err) return this.error(new Error(err));
photoId = results.insertId;
cb(null, photoId);
})
.error(function(err){
cb(err);
});
}
All photos no matter what the size or format cause the same error.
I found that the issue wasn't with AWS and knox, but rather with gm and the fact that GraphicsMagick wasn't installed on my system.
For reference for others, if you're experiencing similar errors, remember that gm doesn't report that GraphicsMagick or ImageMagick are absent when they are absent. I reported this error on the gm module here: https://github.com/aheckmann/gm/issues/273

Resources