nodeJS/ test my js file - node.js

I wrote the next try.js file
var net = require('net')
function create(r){
var server = net.createServer(function(socket) {
socket.write('hello\n');
socket.end('world\n');
});
server.listen(8000);
}
and I want to check if this function works well. So I want to excute it, and then go to localhost:8000 and check if I get the Hello massage. I try to go to dictionary of the try.js file via the node console, but I don't see any option for to do this.

the way you execute a nodejs app is by commandline
node try.js
then you can navigate to localhost:8000 but what you made there arnt a http server so browsers arnt going to get the response. telenet should give you the response 'hello world'. but more likely youre after the http insted of net server

Related

Not able to run node.js program in command prompt

error on executing the programI am beginner to node.js .I have followed all the steps from the below blog:
http://blog.teamtreehouse.com/install-node-js-npm-windows
I got the node version and npm version too.
But not getting the output when I try to run the js file from cmd.
console.log('Node is installed!');
the content of the js file is mentioned in the above line.
Please help me out!!
Thanks in advance
Its Seems there is no hello.js in c drive or in wrong directory
Goto any folder and make your hello.js and then navigate to that folder in cmd then write
node hello.js // .js is optional
in the picture above you can i receive error helloo cant find module becoz i dont have any file with name helloo.js
suppose your file is in D drive then goto D drive and wirte
Your can make simple hello.js server
//Lets re quire/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
//Create a server
var server = http.createServer(function (request, response){
response.end('It Works!! Path Hit: ' + request.url);
});
//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);
});

Http Serving with node.js over the web

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?

Localhost not working with node.js and socket.io

I am trying to learn the basics of node.js and socket.io. I have been using this tutorial http://tutorialzine.com/2012/08/nodejs-drawing-game/
the full code for this problem can be seen in the link above.
I can create a basic web server with node.js and get it to return hello world so I am sure that's installed correctly. However upon installing these packages
npm install socket.io#0.9.10 node-static
and setting up the serverside js as instructed
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
var fileServer = new nstatic.Server('./');
app.listen(8080);
I just get this prompt in my cmd and a constantly hanging web browser, instead of the html page that is meant to be served.I think I may have messed up an install but upon looking at the list of installed packages in npm it states both socket.io and node-static are present.
The code below should be more effective?, it looks like you are missing the handler part. The response must be explicitly ended or browser requests will hang forever like you are seeing. The node-static file.serve method manages the request once you pass it down. The source for .serve is here: https://github.com/cloudhead/node-static/blob/master/lib/node-static.js#L164
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
app.listen(8080);
var file = new nstatic.Server('./');
function handler(request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
}
console.log('started')
Note also that the default file to serve to responses at / is index.html.

In Node js, client=require('socket.io').listen(8080).sockets; is not working for me

Server.js
var mongo = require('mongodb').MongoClient;
client=require('socket.io').listen(8080).sockets;
Then, I ran below command in command prompt:
c:\Users\Admin>node server.js
in command prompt it shows nothing I expect "info - socket.io started"
In the 1.0+ versions of socket.io, there is no default message in the console when socket.io is started. So, seeing nothing in the console is expected.
This was different in older versions of socket.io.
Quoting from the socket.io docs:
Before 1.0, the Socket.IO server would default to logging everything
out to the console. This turned out to be annoyingly verbose for many
users (although extremely useful for others), so now we default to
being completely silent by default.
You can set the DEBUG environment variable if you want socket.io to enable logging. Details are described in the above linked doc.
"info - socket.io started" is not what you should expect.
go to your browser and type http://localhost:8080/ and if it gives your blank white page but not "404" that means your server.js is working perfectly.
If you need some "Thing" to see in your web browser then try this
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(8080);
What you need is <script src="/socket.io/socket.io.js"></script>
Get rid of the localhost param from the script tag.

http server listening in old port

First i installed the node js with webmatrix and ran a sample node js app. the app was assigned a random port. http://localhost:62369/. After that i installed the express module. As said in their doc. i wrote,
var app = express();
app.get('/',function (req, res) {
res.send('hello world!!');
})
app.listen(3000);
Then i restarted the server. The launched browser was still pointing to http://localhost:62369/ instead of port 3000. Moreover http://localhost:3000/ was not working.
I suggest you to run this code so you can see if you have any problem on saving the code with your IDE:
var app = express(),
port = 4555;
app.get('/',function (req, res) {
res.send('hello world!!');
})
console.log("Server is running on " + port);
app.listen(port);
After that, you need to change the port variable only. It's helpful if you comment what you see after running this code on the console.
make sure that you've saved your code in your file (open it with another editor, maybe something's wrong with your editor), close the command line window and open it again. try to run server. I'm sure the problem is not because of node or express. Try to check everything again.
And also run your server with command line:
cd path/folder
node myFile
I don't know what are you using to run server, but if it's something with UI (in comments you mentioned a click) it can cache your code or something like that. So it's safer to run with commend line.

Resources