Which programming language it is? - programming-languages

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);

Related

NodeJS crypto module doesn't work with http module

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.

Node.JS express server creation methods difference

I have recently inherited a project of a Node.JS and Express based API, and I have noticed express server creation is as such (simplified version):
// http is required.
var http = require('http');
var express = require('express');
var app = express();
// Note http is used to create server, and app is used as param:
http.createServer(app).listen(3000, function (request, response) {
console.log('listening on port 3000');
});
Everything works as expected of course.
I have been trying to figure out what exactly is happening behind the scenes here, mostly in comparison to the method in Express API, which shows:
// http is not required.
var express = require('express');
var app = express();
// Note Express is used to create the server:
var server = app.listen(3000, function () {
console.log('listening on port 3000');
})
Note the difference in server creation using http, and using Express directly.
Is there any benefit in using a specific one of the two method? What is the actual difference between the two?
Micro-optimization-wise, is it preferred to avoid requiring 'http', which is probably required by express anyway?
Thanks from ahead!
Both are more or less functionally equivalent, in the second example the express constructor returns a new object which effectively wraps up the http.createServer call internally (i.e. when you call app.listen).
If you are going to use express then you should use it's recommended APIs, the first approach is considered outdated.

Are there any issues with setting custom properties on the node.js request object?

Are the node.js built in objects available for adding custom properties?
var http = require('http');
var server = http.createServer(function(request, response) {
request.myObj = {'lots of info':true}; <-- is it ok to add this object to request?
response.writeHead(200, {
'Content-type':'text/plain'
});
response.end('Hello World!');
});
server.listen(8888);
console.log('Listening on http://127.0.0.1:8888');
Is this considered acceptable or off limits?
This is very commonly done, especially in express/connect apps. Just watch out for name collisions, but otherwise the node community seems mostly totally OK with this based on my experience. If you are paranoid, use a unique namespace like req.MY_APP = {}; and stick all your stuff there.
I did one time encounter a bug when both my application and the strongloop agent tried to set req.graph, but I contacted them and they agreed they should use a less common name for their property.

Setting up Cloud9 SSL App with Node JS

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.

Node.js+express proxy ssl

I'm trying to write a reverse proxy in node.js using express, and it works fine for http requests. The problem is that when requesting https it never responds, and the browser states that the proxy refused to connect.
Here is the working code for http requests:
var app = express(),
http=require('http');
app.configure(function(){ /* express stuff to log and use routes and the like */ });
http.createServer(app).listen(8000, function(){
console.log("Express server listening on port " + 8000);
});
app.all('*', proxy);
var request=require('request');
var proxy=function(req,resp){
var data={
url:req.url,
headers: {
'Connection': 'keep-alive'
}
}
var proxy=request(req.url);
proxy.pipe(resp);
}
Now, as for SSL, i am currently trying with:
var https=require('https'),
fs=require('fs');
https.createServer({
key: fs.readFileSync(__dirname+'/ssl/server.key', 'utf8'),
cert: fs.readFileSync(__dirname+'/ssl/server.crt', 'utf8')
},app).listen(8001, function(){
console.log("Express server listening on port " + 8001);
});
The proxy can be used from anywhere requiring 50.56.195.215:8000 for HTTP and 50.56.195.215:8001 for SSL. It has no security whasoever, so don't log in to anything important =D
I'm using a self signed SSL Certificate, and i guess it's kind of silly of me to try to do such a thing, but i don't have any ideas left :P
My suggestion is use the great existing library node-http-proxy from Nodejitsu. If you want to write your own, at least study their source code academically.
Some notes on your approach above:
You aren't handling HTTP methods other than GET (POST, PUT, DELETE, etc). These exist. You must handle them if you want your proxy to actually work. Every time you call request(req.url), request is making a GET request by default.
For HTTPS, you need to be able to handle HTTP Connects and also impersonate the destination server. You will need to have a Certificate for this.
You can try using this.
https://github.com/noeltimothy/noelsproxy
Copy the directory "magical" that contains a certificate as well as a key and then use noelsproxy. Remember to add the ca.pem to your trusted root store on your system.
If you are using windows, do this:
certutil -addstore -enterprise -f \"Root\" ./magical/ca.pem
Let me know if you have any issues. I'm willing to fix them immediately.

Resources