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);
});
Related
my code goes like this:
var express=require('express');
var app = express();
app.get("\", function(req,res){
res.send("abc");
});
app.listen(process.env.PORT,process.env.IP,function(){
console.log("Server started");
});
I am using goormide and I'm not able to figure out how to preview this I have run the following command on the terminal:
node app.js
where app.js is my file name
However when I run it on url then I'm getting connection refused error.
I would be grateful if anyone could help me to figure out how to run the file on url.
Try starting your node application using the following command:
PORT=3000 node app.js
And update the app.listen part in the following way:
app.listen(process.env.PORT, function(){
console.log("Server started on", process.env.PORT);
});
This will allow you to access your node-application on http://localhost:3000.
Additionally, I guess you've used the wrong slash in your app.get statement.
You would want to change it to:
app.get("/", function(req,res){
res.send("abc");
});
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'm not able to create a localhost for Node.js. Following are the thing I had completed:
I had created a file named server.js and the code is as follows:
var http = require('http');
http.createServer(function(request, respone){
respone.writeHead(200, {'Content-type':'text/plan'});
response.write('Hello Node JS Server Response');
response.end("Hello Sowmay Response ended");
}).listen(7000);
Then I had run this javascript script file on node.js cmd prompt as follows:
C:\Users\Guest\Desktop\First-Node>node server.js
After that I tried opening the local host on my windows PC from this link: http://localhost:7000
Result:
This site can’t be reached
localhost refused to connect.
Where I'm going wrong?
There is a mistake in your code.
I guess your console would output :
undefined has not a function "writeHead"
Becuase respone.writeHead(200, {'Content-type':'text/plan'}); lack a 's'.
Please use:
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {'Content-type':'text/plan'});
response.write('Hello Node JS Server Response');
response.end("Hello Sowmay Response ended");
}).listen(7000);
and try again
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?
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.