How do i specify the path in node/express correctly to read my certificate files? - node.js

This is my file structure simplified
app-folder
-> dist
-> server.js
-> src
-> server.ts
-> key.pem
-> cert.pem
This is the simplified content of the server.ts file to read the certificate files
import fs from 'fs';
const options = {
key: fs.readFileSync('../key.pem'),
cert: fs.readFileSync('../cert.pem')
};
This is the error i am getting
server | internal/fs/utils.js:307
server | throw err;
server | ^
server |
server | Error: ENOENT: no such file or directory, open '../key.pem'
server | at Object.openSync (fs.js:476:3)
server | at Object.readFileSync (fs.js:377:35)
server | at Object.<anonymous> (/usr/src/app/dist/server.js:18:23)
server | at Module._compile (internal/modules/cjs/loader.js:1063:30)
server | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
server | at Module.load (internal/modules/cjs/loader.js:928:32)
server | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
server | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
server | at internal/main/run_main_module.js:17:47 {
server | errno: -2,
server | syscall: 'open',
server | code: 'ENOENT',
server | path: '../key.pem'
server | }
server exited with code 1
I created the key and certificate following this
tutorial
Hoangdv pointed out that i use docker which is correct this is my Dockerfile:
FROM node:14
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN npm install -g nodemon
# Install dependencies
COPY package.json .
RUN npm install
# Bundle app source
COPY . .
RUN npx tsc
EXPOSE 8080
Does COPY . . not copy the certificates already?

Relative paths will be resolved relative to the current working directory as determined by calling process.cwd().
--- node.js docs
import fs from 'fs';
import path from 'path';
const options = {
key: fs.readFileSync(path.resolve(__dirname, '../key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, '../cert.pem'))
};

Try,
var resolve = require('path').resolve
key : resolve('../key.pem')
cert : resolve('../cert.pem')

Related

How to use a global nodejs module?

I got the following error when I try to use esprima. Does anybody know how to fix the problem? Thanks.
$ npm install -g esprima
/usr/local/bin/esparse -> /usr/local/lib/node_modules/esprima/bin/esparse.js
/usr/local/bin/esvalidate -> /usr/local/lib/node_modules/esprima/bin/esvalidate.js
+ esprima#4.0.1
updated 1 package in 0.837s
$ cat main.js
#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:
var esprima = require('esprima');
var program = 'const answer = 42';
console.log(esprima.tokenize(program));
console.log(esprima.parseScript(program));
$ node main.js
internal/modules/cjs/loader.js:960
throw err;
^
Error: Cannot find module 'esprima'
Require stack:
- /private/tmp/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
at Function.Module._load (internal/modules/cjs/loader.js:840:27)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/private/tmp/main.js:4:15)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/private/tmp/main.js' ]
}
I think you installed esprima module in global node_modules.
If you want to use esprima in main.js which is located at /private/temp/main.js,
you should run npm install esprima in /private/temp/ without -g
EDIT - How to require global modules
Way 1
I assume you're in mac system.
Before you run your main.js, run export NODE_PATH=/usr/local/lib/node_modules in the shell, not node.js program.
Then you could require the global modules by const esprima = require("esprima").
By the way, global modules position could be different in the different system.
Way 2
After you knew your global modules position,
you could require it by const esprima = require("/usr/local/lib/node_modules/esprima")

nodejs webapp works locally but not live/deployed

My webapp works locally, but when I attempt to publish it on Azure Web Apps the following error pops up. It is showing Error: listen EADDRINUSE: address already in use :::8080, but the webapp should be using port 3000. I am new to creating webapp, so any help with be appreciated. I used visual studio code to publish the webapp following these settings - https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial-vscode-azure-app-service-node-03. I chose linux and node 12 LTS.
Thank you!
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('myapp:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
2020-07-16T04:23:44.555179603Z / _ \ __________ _________ ____
2020-07-16T04:23:44.555185003Z / /_\ \___ / | \_ __ \_/ __ \
2020-07-16T04:23:44.555189203Z / | \/ /| | /| | \/\ ___/
2020-07-16T04:23:44.555193303Z \____|__ /_____ \____/ |__| \___ >
2020-07-16T04:23:44.555197403Z \/ \/ \/
2020-07-16T04:23:44.555201303Z A P P S E R V I C E O N L I N U X
2020-07-16T04:23:44.555205203Z
2020-07-16T04:23:44.555208803Z Documentation: http://aka.ms/webapp-linux
2020-07-16T04:23:44.555212503Z NodeJS quickstart: https://aka.ms/node-qs
2020-07-16T04:23:44.555216303Z NodeJS Version : v12.16.3
2020-07-16T04:23:44.555220003Z Note: Any data outside '/home' is not persisted
2020-07-16T04:23:44.555223903Z
2020-07-16T04:23:44.902776147Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-07-16T04:23:44.912621757Z Build Operation ID: |bLCEuDLkBSY=.df68a68e_
2020-07-16T04:23:46.477381888Z Writing output script to '/opt/startup/startup.sh'
2020-07-16T04:23:47.136900207Z Running #!/bin/sh
2020-07-16T04:23:47.137528808Z
2020-07-16T04:23:47.137541508Z # Enter the source directory to make sure the script runs where the user expects
2020-07-16T04:23:47.137547108Z cd "/home/site/wwwroot"
2020-07-16T04:23:47.137551508Z
2020-07-16T04:23:47.137555608Z export NODE_PATH=$(npm root --quiet -g):$NODE_PATH
2020-07-16T04:23:47.138703711Z if [ -z "$PORT" ]; then
2020-07-16T04:23:47.138716311Z export PORT=8080
2020-07-16T04:23:47.138721811Z fi
2020-07-16T04:23:47.138725911Z
2020-07-16T04:23:47.139711413Z echo Found tar.gz based node_modules.
2020-07-16T04:23:47.139723313Z extractionCommand="tar -xzf node_modules.tar.gz -C /node_modules"
2020-07-16T04:23:47.139728913Z echo "Removing existing modules directory from root..."
2020-07-16T04:23:47.139733513Z rm -fr /node_modules
2020-07-16T04:23:47.139737613Z mkdir -p /node_modules
2020-07-16T04:23:47.139741713Z echo Extracting modules...
2020-07-16T04:23:47.141077216Z $extractionCommand
2020-07-16T04:23:47.141089216Z export NODE_PATH="/node_modules":$NODE_PATH
2020-07-16T04:23:47.141094416Z export PATH=/node_modules/.bin:$PATH
2020-07-16T04:23:47.141098616Z if [ -d node_modules ]; then
2020-07-16T04:23:47.142105319Z mv -f node_modules _del_node_modules || true
2020-07-16T04:23:47.142117019Z fi
2020-07-16T04:23:47.142122019Z
2020-07-16T04:23:47.142126219Z if [ -d /node_modules ]; then
2020-07-16T04:23:47.142140519Z ln -sfn /node_modules ./node_modules
2020-07-16T04:23:47.142145419Z fi
2020-07-16T04:23:47.149224835Z
2020-07-16T04:23:47.149263335Z echo "Done."
2020-07-16T04:23:47.149334335Z npm start
2020-07-16T04:23:48.155357951Z Found tar.gz based node_modules.
2020-07-16T04:23:48.155942252Z Removing existing modules directory from root...
2020-07-16T04:23:48.164530572Z Extracting modules...
2020-07-16T04:23:51.370781353Z Done.
2020-07-16T04:23:52.204989273Z npm info it worked if it ends with ok
2020-07-16T04:23:52.206185276Z npm info using npm#6.14.4
2020-07-16T04:23:52.206929677Z npm info using node#v12.16.3
2020-07-16T04:23:52.736398696Z npm info lifecycle myapp#0.0.0~prestart: myapp#0.0.0
2020-07-16T04:23:52.746327019Z npm info lifecycle myapp#0.0.0~start: myapp#0.0.0
2020-07-16T04:23:52.759340349Z
2020-07-16T04:23:52.759356549Z > myapp#0.0.0 start /home/site/wwwroot
2020-07-16T04:23:52.759362549Z > node ./bin/www
2020-07-16T04:23:52.759366849Z
2020-07-16T04:23:54.160267671Z Express server listening on port 8080
2020-07-16T04:23:54.170115279Z events.js:287
2020-07-16T04:23:54.170141079Z throw er; // Unhandled 'error' event
2020-07-16T04:23:54.170147979Z ^
2020-07-16T04:23:54.170152379Z
2020-07-16T04:23:54.170156779Z Error: listen EADDRINUSE: address already in use :::8080
2020-07-16T04:23:54.170161379Z at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-07-16T04:23:54.170165779Z at listenInCluster (net.js:1361:12)
2020-07-16T04:23:54.170170179Z at Server.listen (net.js:1449:7)
2020-07-16T04:23:54.170174479Z at module.exports (/home/site/wwwroot/routes/table_routes2.js:63:27)
2020-07-16T04:23:54.170178979Z at Object. (/home/site/wwwroot/app.js:119:37)
2020-07-16T04:23:54.170183679Z at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-07-16T04:23:54.170187979Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-07-16T04:23:54.170192279Z at Module.load (internal/modules/cjs/loader.js:977:32)
2020-07-16T04:23:54.170196679Z at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-07-16T04:23:54.170200879Z at Module.require (internal/modules/cjs/loader.js:1019:19)
2020-07-16T04:23:54.170205179Z at require (internal/modules/cjs/helpers.js:77:18)
2020-07-16T04:23:54.170209379Z at Object. (/home/site/wwwroot/bin/www:7:11)
2020-07-16T04:23:54.170213779Z at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-07-16T04:23:54.170218079Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-07-16T04:23:54.170231979Z at Module.load (internal/modules/cjs/loader.js:977:32)
2020-07-16T04:23:54.170236679Z at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-07-16T04:23:54.170240879Z Emitted 'error' event on Server instance at:
2020-07-16T04:23:54.170244979Z at emitErrorNT (net.js:1340:8)
2020-07-16T04:23:54.170248979Z at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-07-16T04:23:54.170253279Z code: 'EADDRINUSE',
2020-07-16T04:23:54.170257279Z errno: 'EADDRINUSE',
2020-07-16T04:23:54.170261279Z syscall: 'listen',
2020-07-16T04:23:54.170265279Z address: '::',
2020-07-16T04:23:54.170269379Z port: 8080
2020-07-16T04:23:54.170273379Z }
2020-07-16T04:23:54.198841600Z npm info lifecycle myapp#0.0.0~start: Failed to exec start script
2020-07-16T04:23:54.200937601Z npm ERR! code ELIFECYCLE
2020-07-16T04:23:54.201865802Z npm ERR! errno 1
2020-07-16T04:23:54.203466603Z npm ERR! myapp#0.0.0 start: `node ./bin/www`
2020-07-16T04:23:54.203976603Z npm ERR! Exit status 1
2020-07-16T04:23:54.204590604Z npm ERR!
2020-07-16T04:23:54.205116404Z npm ERR! Failed at the myapp#0.0.0 start script.
2020-07-16T04:23:54.211395609Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-16T04:23:54.346290903Z npm timing npm Completed in 2399ms
2020-07-16T04:23:54.347057103Z
2020-07-16T04:23:54.347655604Z npm ERR! A complete log of this run can be found in:
2020-07-16T04:23:54.436761954Z npm ERR! /root/.npm/_logs/2020-07-16T04_23_54_212Z-debug.log
try without normalizeport function

SSL, Express, Nodejs: Error: error:0909006C:PEM routines:get_name:no start line

i tried to add certificates to my nodeJs, Express server like this:
cert: fs.readFileSync('/path/to/private.key'),
key: fs.readFileSync('/path/to/your_domain_name.crt'),
ca: [
fs.readFileSync('path/to/CA_root.crt'),
fs.readFileSync('path/to/ca_bundle_certificate.crt')
]
and i got this error
c.context.setKey(key, passphrase);
^
Error: error:0909006C:PEM routines:get_name:no start line
at Object.createSecureContext (_tls_common.js:151:17)
at Server.setSecureContext (_tls_wrap.js:1155:27)
at Server (_tls_wrap.js:1033:8)
at new Server (https.js:65:14)
at Object.createServer (https.js:89:10)
at Object.<anonymous> (********)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11 {
library: 'PEM routines',
function: 'get_name',
reason: 'no start line',
code: 'ERR_OSSL_PEM_NO_START_LINE'
}
does anyone faced this problem please?
Thanks by advance
Your may write wrong file name in cert and key section.
key: section need private key filename and cert: section need certification filename.
I switched your filename in cert and key section.
cert: fs.readFileSync('/path/to/your_domain_name.crt'),
key: fs.readFileSync('/path/to/private.key'),
ca: [
fs.readFileSync('path/to/CA_root.crt'),
fs.readFileSync('path/to/ca_bundle_certificate.crt')
]

installing and running browsershots

I tried to install Brwoswershots (https://github.com/spatie/browsershot) and I did this exactly by following the instructions on github.
Installing NodeJS and npm via yum
Installing browswershoots via composer: composer require
satie/browsershot
crated a file with
use Spatie\Browsershot\Browsershot;
$pathToImage="img/testasdf.png";
// an image will be saved
Browsershot::url('https://example.com')
->setScreenshotType('jpeg', 100)
->save($pathToImage);
It is on http://textblob.com/browsershots.php and brings the error.
Fatal error: Uncaught
Symfony\Component\Process\Exception\ProcessFailedException:
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node
'/home/textblob/public_html/vendor/spatie/browsershot/src/../bin/browser.js'
'{"url":"https:\/\/example.com",
"action":"screenshot",
"options": {
"type":"jpeg",
"path":"img\/testasdf.png",
"quality":100,
"args":[],
"viewport":{"width":800,"height":600}
}
}'"
failed. Exit Code: 1(General error) Working directory:
/home/textblob/public_html
Output: ================ Error Output: ================
/home/textblob/public_html/vendor/spatie/browsershot/bin/browser.js:5
const getOutput = async (page, request) =>
{ ^ SyntaxError: Unexpected token ( at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMa in /home/textblob/public_html/vendor/spatie/browsershot/src/Browsershot.php
on line 565
I could solve this by downgrading to boxshot v2, witch works without puppeteer and so its not a fix, but working for me.

How to deploy a phantomjs node app on AWS Lambda?

I threw together a small Lambda function together to crawl a website using the SpookyJS, CasperJS, and PhantomJS toolchain for headless browsing. The task is quite simple, and at some point a few months ago it was working in Lambda. I recently had to change a few things around and wanted to work on the project again, but started fresh and had trouble getting Lambda to run without erroring in any capacity. My question is how can I run phantomjs in Lambda?
The example code I am running is:
spooky.start('http://en.wikipedia.org/wiki/Spooky_the_Tuff_Little_Ghost');
spooky.then(function () {
this.emit('hello', 'Hello, from ' + this.evaluate(function () {
return document.title;
}));
});
spooky.run();
The error I am getting in Lambda is:
{ [Error: Child terminated with non-zero exit code 1] details: { code: 1, signal: null } }
I have followed a variety of procedures to ensure everything is able to run on Lambda. Below is a long list of things I've attempted to diagnose:
Run locally using node index.js and confirm it is working
Upload package.json and the js file to an Amazon Linux EC2 instance for compilation as recommended for npm installation calls and described here
Run npm install on the ec2 instance, and again run node index.js to ensure the correct output
zip everything up, and deploy to AWS using the cli
My package.json is:
{
"name": "lambda-spooky-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"casperjs": "^1.1.3",
"phantomjs-prebuilt": "^2.1.10",
"spooky": "^0.2.5"
}
}
I have also attempted the following (most also working locally, and on the AWS EC2 instance, but with the same error on Lambda:
Trying the non -prebuilt version of phantom
Ensuring casperjs and phantomjs are accessible from the path with process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/.bin';
console.log( 'PATH: ' + process.env.PATH );
Inspecting spawn calls by wrapping child_process's .spawn() call, and got the following:
{ '0': 'casperjs',
'1':
[ '/var/task/node_modules/spooky/lib/bootstrap.js',
'--transport=http',
'--command=casperjs',
'--port=8081',
'--spooky_lib=/var/task/node_modules/spooky/lib/../',
'--spawnOptions=[object Object]' ],
'2': {} }
Calling .exec('casperjs') and .exec('phantomjs --version') directly, confirming it works locally and on EC2, but gets the following error in Lambda. The command:
`require('child_process').exec('casperjs', (error, stdout, stderr) => {
if (error) { console.error('error: ' + error); }
console.log('out: ' + stdout);
console.log('err: ' + stderr);
});
both with the following result:
err: Error: Command failed: /bin/sh -c casperjs
module.js:327
throw err;
^
Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
2016-08-07T15:36:37.349Z b9a1b509-5cb4-11e6-ae82-256a0a2817b9 sout:
2016-08-07T15:36:37.349Z b9a1b509-5cb4-11e6-ae82-256a0a2817b9 serr: module.js:327
throw err;
^
Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
I found the issue to be that including the node_modules/.bin in the path works on both local and ec2 machines because those files simply point to the action /bin folders in each respective library. This breaks if calls within those files use relative paths. The issue:
[ec2-user#ip-172-31-32-87 .bin]$ ls -lrt
total 0
lrwxrwxrwx 1 ec2-user ec2-user 35 Aug 7 00:52 phantomjs -> ../phantomjs-prebuilt/bin/phantomjs
lrwxrwxrwx 1 ec2-user ec2-user 24 Aug 7 00:52 casperjs -> ../casperjs/bin/casperjs
I worked around this by adding each library's respective bin to the lambda path in the Lambda handler function:
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT']
+ ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/phantomjs-prebuilt/bin'
+ ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/casperjs/bin';
And this will now run phantom, casper, and spooky correctly in Lambda.

Resources