I'm trying to upload two node.js files to koding.com and run them.
index.js contains:
var server = require("./server");
server.start();
And server.js
var http = require("http");
var url = require("url");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(6665, '0.0.0.0');
console.log("Server has started.");
}
exports.start = start;
I typed in vm terminal jstq#vm-0:~$ node Web/IkapNodeJS/index.js
it gives me Server has started.
If im going to http://jstq.kd.io/IkapNodeJS/index.js - i see index.js contains.
When im adding :6665 to that url - url not found.
How do i see page with hello world?
If you're running your application on 6665, the you would access it with http://jstq.kd.io:6665/. You should see your Hello world there.
Node does not run like a cgi script; you don't point to the file to run it, you run it with a process (node). When the server is running on a specific port, the content will be available on any address/hostname that points to that machine so long as you specify the proper port.
HTH,
Aaron
Related
This is the stock node app from Microsoft Documentation.
Just followed instructions here ->
https://learn.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs
All it has in index.js is this
var http = require('http');
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World!");
});
var port = process.env.PORT || 1337;
server.listen(port);
console.log("Server running at http://localhost:%d", port);
App runs ok on local machine and give expected output.
Only when I host on Azure and access the app,
my response looks like this
e
Hellooo World!
0
If I change the 'Hello World!' to some other string,
the 'e' part of response changes along with the string.
The 0 stays.
Any idea why I'm getting the extra lines above and below the hello world line?
Answering my own question -
I had "application insights" turned on in the app I manually created.
That was causing the extra characters in output.
Turned that off and everything is fine
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 am new in NodeJS, I want to create a simple http server, Just send a 'Hello World' back to the client. I did something. If anyone, can check out my code if I did it right, if not add yours, I will be really appreciated.
Here is my code.
var http = require("http");
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World!");
response.write("</head>");
response.write("<body>");
response.write("Hello World!");
response.write("</body>");
response.write("</html>");
response.end();
});
server.listen(80);
console.log("The Server is listening now...");
Your node.js is fine, however, you are missing a closing title tag in your html, eating up the rest of your page. Modify your title line so it is like so:
response.write("<title>Hello World!</title>");
and you should see output on the browser.
Happy Programming! :)
I'm following along in a MongoDB course and came to a basic Node Hello world server tutorial:
Basically it just displays Hello World on localhost:8000
So I typed out the code below and when I tried to run it, ran into the following error:
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello, World"\n);
});
server.listen(8000);
console.log("Server running at http://localhost:8000");
If I remove the \n it will work fine, just curious as to how does the teacher get his to work with it and no errors?
It's just a typo. It should be: response.end("Hello, World\n");.
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.