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
Related
I'm running a nodejs application on my virtual host.
I just want to see the results of my index.js port 3000 on browser (I have tested all open ports) , but I will get connection time out with domain or server Ip.
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var message = 'It works!\n',
version = 'NodeJS ' + process.versions.node + '\n',
response = [message, version].join('\n');
res.end(response);
});
server.listen(3000,function(){
console.log("Listening on port : " + 3000);
});
And I will start with node index.js
I can see the results with ssh command
curl https://localhost:3000
But when I can't see the results in browser
Any idea for this?
You trying to access server via https protocol, however your server supports http
You need to use the default port by node js and the code you need to write is as follows
server.listen(3000 || process.ENV.PORT,function()
{
console.log("Listening on port : " +process.ENV.PORT);
});
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
server.listen(8080);
I am not getting result for the above nodejs code i am executing the code like this node hw.js from git bash
I am new to nodejs
Your code is creating an HTTP server on port 8080.
For every request it will respond with 'Hello Http'
Try opening http://localhost:8080/ in your browser and you will see it.
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);
});
I have the following simple http-server setup using node.js:
var http = require('http');
var port = 12311
http.createServer(function (req, res) {
console.log("Incomming request from " + req.connection.remoteAddress);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end("test string");
}).listen(port);
console.log("Listening on " + port);
As you can see, when a request comes in, I log it to the console. Now when I browse to localhost:12311 the console shows that two connections have come in:
"E:\Program Files\nodejs\node.exe" hello-world-server.js
Listening on 12311
Incomming request from 127.0.0.1
Incomming request from 127.0.0.1
Why is this?
It's usually the request for the favicon.ico. Even if you don't have one, it's requested as the norm defines a default file path if you don't set the relevant <link rel="shortcut icon"... in the header.
The best ways to find about the requests are :
client side, by opening the developer tools and looking at the network tab.
server side, by logging req.url
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.