What is difference between NodeJS http and https module? - node.js

I am using http module in my project but most of my 'post' requests are blocked by postman.
I read it is a ssl issue,after some research i found another module named https.
Here is my current code.
var http = require('http');
var server = http.createServer(app);

Hei, make sure that the interceptor in Postman is off (it should be in the top, left to the "Sign in" button)
And related to https, as stated in Node.js v5.10.1 Documentation
HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a separate module.
I used it once to make requests from my server to other servers over https (port 443).
btw, your code shouldn't work, try this
const http = require('http');
http.createServer( (request, response) => {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
and use http://127.0.0.1:8124 in Postman
..hoped it helped

const http = require('http');
http.createServer( function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');

The difference between HTTP and HTTPS is if you need to communicate with the servers over SSL, encrypting the communication using a certificate, you should use HTTPS, otherwise you should use HTTP, for example:
With HTTPS you can do something like that:
var https = require('https');
var options = {
key : fs.readFileSync(path.resolve(__dirname, '..', 'ssl', 'prd', 'cert.key')).toString(),
cert : fs.readFileSync(path.resolve(__dirname, '..', 'ssl', 'prd', 'cert.crt')).toString(),
};
var server = https.createServer(options, app);
server = server.listen(443, function() {
console.log("Listening " + server.address().port);
});

Related

ERR_CONNECTION_REFUSED when redirecting http to https using nodejs

I'm trying to redirect http requests to https using nodejs.
Here is my code so far. I have included all key, certs etc.
http and https ports are ON ( custom ports, not defaults)
Now on hitting http, site does get redirected to https but it shows err as shown in screenshot attached. screenshot.
certs are valid ( see bottom of screenshot ) but still it says THIS PAGE IS NOT SECURE.
Please guide me where i'm wrong and possible solution.
I have searched here but couldn't get solution to this. Thanks.
var fs = require('fs');
var http = require('http');
var http_port =any_port;
var app = require('express')();
// HTTPS definitions
var https = require('https');
var https_port =another_port;
var options = {
key: fs.readFileSync('example.key'),
cert:fs.readFileSync('example.crt'),
ca: fs.readFileSync('bundleexample.crt')
};
app.get('/', function (req, res) {
res.end('Hello https World!');
});
https.createServer(options, app).listen(https_port, function () {
console.log('https port ' + https_port);
});
// Redirect from http port to https
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'].replace(http_port,https_port) + req.url });
res.end();
}).listen(http_port);

Getting Node, Running on OS X, to Respond to HTTPS Connections

I have a small node program.
console.log("Program Started");
var http = require('http');
//Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Processing Request for URL" + request.url);
response.writeHead(200, {"Content-Type": "text/html"});
response.end("Hello World \n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
console.log("Program Ended");
If I run this program on my MacBook and access the following URL, I see my Hello World message.
http://localhost:8000/
However, I'd like to have this URL responding to HTTPS connections as well.
https://localhost:8000/
How can I do this with NodeJS?
Can the native http library do this for me? Or will I need Express?
Yes you can simply do It via https module :
1 you will have to create certificats , you can follow this auto here for macOS
https://stackoverflow.com/a/42298344/8395557

why does the node.js file stop working when I add a hostname to the file?

I'm running an apache and ubuntu server on aws.
Both the below files run but when I access the server only the top one returns Helloworld. Why is that?
var http = require('http');
var port = 3000;
var server = http.createServer(function(req, res) {
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello World</h1>');
});
server.listen(port);
The bottom file runs but doesn't when I hit the server I get an error. I tried replacing hostname with the ip of the server, I also tried making the host name 127.0.0.1 that didn't work either. The error I get is
"This site can't be reached, <ip> refused to connect, ERR_CONNECTION_REFUSED"
is the ip of the server.
var http = require('http');
var hostname = 'localhost';
var port = 3000;
var server = http.createServer(function(req, res) {
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello World</h1>');
});
server.listen(port, hostname);
Cheers
If you're listening on localhost, you have to make the request from the same machine/server and not from the outside.

stripping port number from domain in aws server

http://www.basicspace.org:5000/#/
Here is my url, I want to run the app just on the http://www.basicspace.org without the port number. I have seen lots of tutorials but none got into my head so asking it here for better solutions.
If it is just a single EC2 Instance which is running NodeJS then. If it listens behind and ELB, then PORT source and destination to be configured in ELB.
var util = require('util');
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write('Hello World');
response.end();
}).listen(80);

node.js 8080 port isn't listening

I'm following the node.js tutorial in here,
http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/
this is the code,
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.sendHeader(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.close();
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
in here, it says run like this url,
server's ip:8080/
but if i do this,
it just shows, cannot connect to this url.
i opened 8080 port in the server.
===========================
I'm assuming something is screwed up with codeigniter url helper...
The tutorial may be using an incorrect or deprecated method. Replace
response.sendHeader(200, {"Content-Type": "text/html"});
with
response.writeHead(200, {"Content-Type": "text/html"});
and
response.close();
with
response.end();
I Agree with the answer of Third .... make those changes and if it is local use this URL
http://127.0.0.1:8080/
But
If you are running your server not on localmachine but on something like webserver(AWS), You have to let the security of AWS firewall to allow the port to be public on the internet and also remember to use the AWS instance URL
http://AWSinstanceURL:portno/
Use this one
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
Now open http://localhost:8080 and u will get your result.

Resources