is there any way to know the remote IP adress form a request on on the http server?
Using "net" for a socket connection is socket.remoteAddress but for the http server I get undifined for the same .remoteAddress
Regards
I'm just adding this because I came across this article and it still took me a little bit of time to figure out it was as you see in the following:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello ' + req.connection.remoteAddress + '!');
console.log("request received from: " + req.connection.remoteAddress);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
You can use the request.connection method to retrieve the net.Socket object, where you can then use your socket.remoteAddress property.
Related
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 am stumped.
I have a node js server running on a subdomain.
Every time i try to access it from a browser i get ERR_CONNECTION_REFUSED error.
Here is my server code
const http = require('http');
http.createServer( function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124, "my-server-ip");
console.log('Server running at http://127.0.0.1:8124/');
Additional Information:
I am running a VPS with Hostgator.
The subdomain shares an IP with main domain; something like 412.x.xx.xxx .
insert console log inside create server function. Then you can identify error for this. Also you can use error response.
const http = require('http');
http.createServer( function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
console.log('Server running at http://127.0.0.1:8124/');
}).listen(8124, "my-server-ip");
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.
I have made an example script from Node.js website main page like so:
var http = require('http');
http.createServer(function (req, res) {
console.log("We are connected");
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Any ideas why each time I connect to 127.0.0.1:1337 (via chromium) I get 2 responses saying "We are connected"??
There are two requests by the browser. One for url / and another for /favicon.ico. Try to output the request url.
console.log(req.url);
I am looking for a way to set dynamic url http://host.com/dynamic_value-123/ and be able to get these values (123)
Considering you have the most basic example of a server with Node.js, you can achieve that with req.url.split('-')[1]:
var http = require('http');
http.createServer(function (req, res) {
console.log(req.url.split('-')[1]);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');