I have just started exploring node.js. Have installed msi file on my windows server. My code below returns me expected output in command window
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at `http://127.0.0.1:8081/`');
But when i type http://127.0.0.1:8081/ in my browser i dont get any output. When i see console i get below error
Failed to load resource: the server responded with a status of 403 (Forbidden)
What i wrong and how to fix? I am following this link
probably like my current PC, your's too must be running McAfee or some other program is already using port 8081, you got two options:
stop McAfee or the other program running on that port
listen to a different port on your node server
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(300);
console.log('Server running at http://127.0.0.1:300/');
I changed it to 300 from 8081 and it worked.
http://127.0.0.1:300/ this url gives the desired output.
Related
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
I have started a server at local host:8081 as
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
After starting the service, I am not able to access internet. please help me with this.
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");
On a node HTTP server I'm spawning a process and streaming the output to the response.
When the process returns I'd like to indicate to the client if an error occured. Obviously I can't set the HTTP status code as the headers were already sent.
Is there a way to abort the connection?
E.g.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World\n');
// how do I abort the request at this point
// and indicate an error to the client?
// e.g. curl should return != 0
res.end();
}).listen(1337, '127.0.0.1');
I found this in google groups, you can use
either req.client.destroy();
or res.connection.destroy();
curl will then report
curl: (18) transfer closed with outstanding read data remaining
var thirdPartyApp = $express();
thirdPartyApp.use('/error/', function (req, res) {
console.log('error');
res.writeHead(200);
res.write('aye');
throw 'booboo!';
res.end();
});
On expressjs this does not kill the node process (probably just need to bind to the error event) but does immediately kill the response, indicating an error to the user without a timeout.
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);