I'm running a simple web server using NodeJS (to which I'm using the http module) and I am trying to create a username/password login system using the crypto module. Specifically, I'm using crypto.scryptSync() to hash the passwords. However, whenever I run these two together, such as in below:
const http = require('http');
const crypto = require('crypto');
console.log(crypto.scryptSync('password', 'salt', 64));
const server = http.createServer((req, res) => {
res.writeHead(200, {
'content-type': 'text/plain'
});
res.end('ok');
});
server.listen(80);
I get an error stating that TypeError: crypto.scryptSync is not a function and I don't know why.
Is there something incorrect in the way that I import the modules or are they just incompatible?
I'm running NodeJS v12.18.3, but the same still happens on the latest version.
Turns out crypto.scryptSync() was released in NodeJS v10.5.0. Since I was running my script as sudo, an older version of NodeJS was being used, causing the error.
Related
I'm going insane trying to get a super basic wss:// functioning in NodeJS for the last 2 days. I've tried quite a few methods and libraries but I can't seem to get the websocket server attached to an https instance. I have no problem leveraging regular old http and attaching it to that instance. I don't get any errors in my debug console.
I've created both self-style type certs (Create Key + CA, create CSR,
sign it, use new server cert), and (Create Key + self-signed Cert,
use them).
I've tried disabling TLS verification via env var:
NODE_TLS_REJECT_UNAUTHORIZED="0"
I've tried both ws, and websocket libraries and many different combos
of basic ws creation vs server attaching methods.
I've built a VM of Ubuntu 21.04, installed dependencies and vscode
just to rule out my OS. Same issue here.
Tried using node versions 14 + 16.
:Package Deps:
"websocket": "^1.0.34",
"ws": "^8.0.0"
:server.js:
const fs = require('fs');
const WebSocket = require('ws');
//HTTPS
const https = require('https');
const server = new https.createServer({
key: fs.readFileSync('./config/certs/key.pem'),
cert: fs.readFileSync('./config/certs/cert.pem')
});
//HTTP
// const http = require('http');
// const server = new http.createServer();
const wss = new WebSocket.Server({server});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('hello from server!, the time is: ' + timestamp());
});
});
//Helper function to create a timestamp
function timestamp() {
return (new Date)
.toISOString()
.replace(/z|t/gi, ' ')
.trim()
};
//Start the server
server.listen(3000);
I'm suspecting some underlying compatibility issues between node and dependencies or something...Any advice would be much appreciated. I'm not too familiar with debugging internal modules so if there are some command line switches I should add to node/nodemon please let me know. I have --inspect and --trace-warnings enabled at the very least.
I just figured it out and as usual it was something simple and overlooked. I've been using Firefox with the Weasel client add-on to test websockets. I had imported my self-signed cert along with the root CA cert I had created into Firefox. Even though it was imported, I still had to navigate to the HTTPS url and acknowledge the wonderful yellow border popup. As soon as I clicked on "Accept risk and continue" I tabbed over to Weasel and it established a connection to wss://localhost:3000 with no problems.
Even though the cert is whitelisted I still receive the warning page and have to acknowledge it. Next time I'll try a different client like one built in another language (Python, .NET...). Never would have thought it to be a browser issue but it makes sense with the way ssl/tls works.
New to Node here, trying for the last 3 days straight, no clue.
Read all similar issues and tried literally everything I could find, no luck.
Suspecting something that is not common or related to my machine or code.
Issue: trying to fetch data in node.js from postgres db - to console (at least) so to render it later as HTML
Database has table name: students on heroku, and has records
Also locally on my macOS, I have postgres installed with simple data in a table called students
I couldn't fetch any data, no error, no idea how to track it!
Tried creating connection with pool, client.. also used heroku guide here exactly
Literally everything that other users mostly encountered
DATABASE_URL environment variable is ok, if i echo $DATABASE_URL in Terminal:
postgres://xnlkikdztxosk:kuadf76d555dfab0a6c159b8404e2ac254f581639c09079baae4752a7b64a#ec3-52-120-48-116.compute-1.amazonaws.com:5432/uytnmb7fvbg1764
When i run 'node app.js' server starts ok on port 3000, I can use postman on the root '/' OK and it works, it returns back the json info and console.log
If i try postman to '/students' then it tries forever, no results, no error, nothing
Tried also with my local installation of postgres, same thing
My modules are ok, and I run npm install several times
Thought could be my mac firewall, i turned it off completely
Also tried this, nothing prints out or no idea where to track it:
process.on('uncaughtException', function (err) {
console.log(err);
});
Guide or steps to follow in order to track issues like this will be highly appreciated
app.js file:
const express = require('express')
const bodyParser = require('body-parser')
const { Client } = require('pg');
const app = express()
const PORT = 3000
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
client.connect();
app.get('/', (req, res) => {
res.json({ info: 'Info: This is the root directory' });
console.log('main directory')
})
app.get('/students', (req, res) => {
client.query('SELECT * FROM students;', (err, res) => {
if (err) throw err;
for (let row of res.rows) {
console.log(JSON.stringify(row));
console.log('WHOOOOOO, finally!');
}
client.end();
});
});
app.listen(PORT, function(){
console.log('Server running on port 3000');
});
Well, my node version was for some reason v14, not sure how that happened, but the most stable version in node site is 12, so I installed v12 and the connection to pg worked locally and remotely on heroku.
This is just to highlight what worked with me after trying 4 days straight.
However, that may trigger for you different issue like like this which I'm facing:
DeprecationWarning: Implicit disabling of certificate verification is deprecated and will be removed in pg 8. Specify `rejectUnauthorized: true` to require a valid CA or `rejectUnauthorized: false` to explicitly opt out of MITM protection.
All answers found so far point to: pg module already fixed in v7.18.1 but for some reason I can't force package.json to take that version, it jumps me to version 7.18.2
Tried that along with latest version 8.3 same issue with heroku, but locally the message doesn't show
Not big deal though, connection works for now until figuring it out.
I think the issue here is that you don't send back any response in the /students route .Notice the / route u have a res.json which sends back a response but in /students route i don't see where your response is sent and that's why you wait forever
So I'm trying to get a basic https server running in node, and I'm completely stuck. I've generated a self signed certificate and key with openssl, and tried the basic way to create the server and another hundred of them, but no matter what I do my browser just tells me "The connection has been reset" when I try to connect, and the server doesn't even execute the callback function for the request, as if it has never arrived.
The network inspector in Firefox Developer Edition shows no response at all from the server, and inspecting my loopback interface using Wireshark I've found the server is sending an "end" package right after acknowledging the browser's request.
I really have no idea on what can be wrong, as I've tried with example codes from many tutorials and all of them produce the same output.
This is an example of some very basic code that throws no errors, but also apparently doesn't work:
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('sslcert/key.pem'),
cert: fs.readFileSync('sslcert/server.crt'),
rejectUnauthorized: false
}
https.createServer(options, (req, res) => {
console.log('request received')
res.writeHead(200)
res.end('hello')
}).listen(8443).on('error', (error) => {
throw error
})
I had the same problem, and for me, accessing the page with the https:// -protocol specified did the job in Firefox.
So use
https://localhost:8443
instead of just
http://localhost:8443
I found this script on tenable "getting hired" website...
Just for curiosity, do you know what language it is?
It is node.js a server side javascript framework.
More details: Reference to this here. It is used to build sever side logic to implement parallel services with other server. Its speciality is high throughput for complex logics.
That would be node.js a server-side library written in javascript.
It is on node.js
As in the Code it Returns a new HTTPS web server object. The options is similar to tls.createServer(). The requestListener is a function which is automatically added to the 'request' event.
Here are the Example Code of the Above Code to get Some Brief Idea About it:
// curl -k https://localhost:8000/
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
I've been playing around with the Cloud9 IDE and am having a great time with it. However, I'm trying to setup a simple https server with node js and I can't seem to get it to work. When I run the page, Cloud9 says 'Running Node Process' but when I visit the url that the server is supposed to respond to: https://workspace.user.c9.io the page says
Service Temporarily Unavailable
The server you are trying to contact is down either because it was stopped or is unable to service your request due to maintenance downtime or capacity problems. Please try again later.
node-web-proxy/0.4 Server at project-livec9f70a01ca28.rhcloud.com Port 8000
I created a test certificate with OPENSSL and am using the following code to set up my server. I can confirm that the OPENSSL certificate was built correctly.
var https = require("https");
var fs = require("fs");
var url = require("url");
var options = {
key: fs.readFileSync('certs/cert.pem'),
cert: fs.readFileSync('certs/cert.pem')
};
// create a server
https.createServer(options, function(req, res) {
console.log("This works!");
res.writeHead(200);
res.end("Hello world from Cloud9! Url:"+req.url);
}).listen(process.env.PORT);
Thank you for your help!
you need .listen(process.env.PORT,process.env.IP);
It should say that when you start a program.