Node.js htaccess like get rules - node.js

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

Related

Receiving error message about syntax nodejs

I am new to Node js. I am following this tutorial online. I was trying to test the code but I get an error every time about line 1 syntax error and I got the code from the tutorial so I am not sure what the problem is? Can someone help me please? Thanks in advance
//Here is the code
myfirst.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

Cannot Connection to Port at Simple Node Js server

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

Difficulty understanding nodejs documentation

I'm really confused about this section
http://nodejs.org/api/http.html#http_http_createserver_requestlistener
The requestListener is a function which is automatically added to the 'request' event.
What does the term "added" specifically mean?
Also for here
http://nodejs.org/api/http.html#http_event_request
What does the the code directly beneath mean function (request, response) { }? Does it mean that that function gets passed each time there is a request?
The requestListener is a lsitener that listens to the 'request' event. Each time a request event is emitted, the requestListener is executed. You pass a function.
That function you pass, should match:
function (request, response) { }
I believe there is an example at the main page of nodejs.org.
var http = require('http');
http.createServer(function (req, res) {
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/');
So each time a request-event is emitted, this function is 'called'.
function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
With req and res a parameters. (Request and response).
If it is any help the statement
var app = http.createServer( function reqlistener(request, response){...} ).listen(1337);
where the function reqlistener is the requestListener argument, is equivalent to the following
var app = http.createServer().listen(1337);
app.on('request', function reqlistener(request, response){...} );
So it is just a shortcut for providing a listener for event request during server start itself. The event request is emitted for each request once when received by the server.

Node.js example gives 2 responses?

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

Remote IP from request on http server

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.

Resources