I'm learning Node.js and I've installed VirtualBox and there ubuntu server 14.04. Node.js is installed as well on ubuntu. I'm doing an exercise in which I created a server which is accessible from guest_localhost:3000 (in ubuntu). The thing is I've not installed any GUI (and I wish to continue in that way). I want to test that the server I created using Node.js is running. To do that I just need to go to server_localhost:3000.
Apparently everything is ok (for example ping host to guest ip), but for some reason it´s not working.
ping ok __ not working
`var http = require('http');
var handleRequest = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to Node Essential Training\n');
};
var server = http.createServer(handleRequest);
server.listen(3000, 'localhost');`
I'll appreciate any help.
Thanks in advance guys.
If you do not have any GUI to visibly verify your page layout, you should be able to access it from your windows machine if they are in the same local network by simply accessing the url http://your_ubuntu_local_ip:3000 where your_ubuntu_local_ip is the local ip your ubuntu machine has internally in your local network. You can find this ip by typing ifconfig in your ubuntu terminal and looking for the ip the network adapter you are using has.
It's solved. The problem was in the code to create the server after all. As I'm learning JavaScript and Node.js I'm watching a tutorial in Lynda.com. The code which doesn't work comes from Lynda. The other from this url: http://blog.modulus.io/build-your-first-http-server-in-nodejs
// THIS CODE DOES NOT WORK
//var http = require('http');
//
//var handleRequest = function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Welcome to Node Essential Training\n');
//};
//
//var server = http.createServer(handleRequest);
//
//server.listen(8080, 'localhost');
//THIS CODE WORKS
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=3000;
//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);
});
Related
So I have this script
const hostname = 'localhost';
const port = 2525;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
File named nodeTest.js and I have all this on a webhost where I also have my domain. Now running this script through ssh "node nodeTest.js" creates server and I can access it with another ssh terminal with curl call. I get response "hello world" as I should.
Now the problem is I cannot access it normally through my domain link.. like somethingsomething.com
I also have option to create node application through a cpanel but when I create it i get no response going to specified link
I just started with nodejs so maybe I am missing something obvious. There is also help site on my webhost web that has tutorial how to set this up and I do as they say but i get no console.log msg.
I also tried setting server to 8080 port but I get error already in use.
Would appreciate if someone would point me in the right direction.
I figured it out.. with the help of this site
https://www.a2hosting.com/kb/cpanel/cpanel-software/create-application-with-nodejs-selector
I was missing package.json part
I have developed MERN app in my local system. I am trying to host it AWS-EC2 (Free Tier).
Never thought deploying would be so painful.(Well, i am beginner to Node. Earlier i have worked with php. I found it easier to integrate)
I referred this article to install Node and Express. Node is successfully installed. I created sample file 'test.js' with following code:
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'test/plain'});
res.end('Hello world!\n');
}).listen(port);
console.log('Listening on port',port);
After executing
node test.js
the browser downloads a file with no extension. Opened in editor and it has 'Hello World' in it. Nothing else. I am pretty sure i have followed all steps properly. But still i ended up 'Downloading the file'
Can anyone help me with this issue?
BONUS QUESTION : How do i deploy MERN app in AWS EC2?
Have a nice day
Thanks
try this (you forgot to add the listen callback, and a typo in content type):
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello world!\n');
}).listen(port, err => {
if (err) throw err
console.log('Listening on port',port);
})
Use appropriate content type for your html page.
res.writeHead(200,{'Content-Type':'text/html'});
For a viewable html code, text/html should be set as content type instead of test/plain.
I started an simple express server on MacOS High Sierra. localhost:3000 and 127.0.0.1:3000 work fine. However, when I accessed the server via http://192.168.x.x:3000 from another computer, I didn't get any response (ERR_EMPTY_RESPONSE).
I tested the server on another MacBook (MacOS Sierra) and a Windows computer, everything works fine.
So I suppose there are something wrong with MacOS High Sierra.
Any help would be welcome. Thanks in advance.
Here is the code :
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
UPDATE MacOS
I have tried to reinstall node, npm, change share settings, but they don't work, neither.
Finally, I updated MacOS (10.13.4 --> 10.13.5) and now other computers can access my server like a charm.
----------Update -------------
After a few (happy) day, the issue raised again.
Fortunately , now I pretty sure that ESET EndPoint Security was the problem since it's the last application I installed.
So I open the ESET firewall and create a new rule which allow local network address to connect via TCP&UDP.
The server once again works like charm.
Hope this help the other.
Refer to https://nodejs.org/en/docs/guides/getting-started-guide/
Once you have installed Node, let's try building our first web server. Create a file named "app.js", and paste the following code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
After that, run your web server using node app.js, visit http://localhost:3000, and you will see a message 'Hello World'
This is a very basic question. But I have looked and can't seem to find any tutorials that walk through this step. Everything either stops just before this step OR starts just after it.
I have launched an AWS server (Windows_Server-2016-English-Full-Containers-2016.10.18 (ami-d08edfc7)) and installed node in the default directory.
I have put a file into the following content:
var http = require('http');
const PORT=8080;
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
});
I then open CMD, navigage into the directory where node is installed and run the program with:
node myServer.js
Next I open a browser and navigate to http://localhost:8080 and I am served some content. Terrific.
My question is how do I go about making a request of that newly installed server from another machine over the internet. My primitive guess was to simply navigate to the AWS machine's public IP, as displayed in the AWS console and include the port number.
So for example if my IP were 55.173.140.15 I would type in the address http://55.173.140.15:8080 and expect to see the page. That is not working. So what configuration step am I missing?
Recently I installed node on new server , and create this simple program.
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.end();
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
When I run this in browser http://newserver:8080/ version 0.4.10 not working .
http://oldserver:8080/ working .version 0.5.0 pre .
Why this happening?.
You've referred to a new and an old server, which suggests to me you're not running this from localhost. In that case, have you verified that both servers have the same firewall rules? Specifically, is port 8080 allowing external traffic on the new server?
Is someone already listening on 8080? Try telnetting to that port and see if it works.