Node.Js Cannot find module, module.js:471 - javascript

I just started learning node.js today. I just downloaded the most recent version. I have run into several issues along the way but i was able to solved most of them besides one error which I keep getting:
Error: Cannot find module 'C:\Users\Dennis\sample.js'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
This is my code, a sample js code I got from W3schools.
<script>
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
</script>
My nodejs folder is in C:\Program Files\nodejs.
I put this sample.js file inside the folder and I had no luck. I even copy this sample file and put it under all the path from C to nodejs, still nothing.
Any tip will be appreciated.

I'm fairly sure even w3Schools wouldn't put that in their tutorials.
Your file should contain only:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
<script> tags belong in html pages (or templates, etc.).
Also, no the file name does not need to end with .js for node to interpret it. What does happen is that require() statements that ask for a file and do not specify an extension will default to a file of the same name that ends with .js.

you have to run in console, for example I have linux and run in console:
nodejs nombre.js
where nombre.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
then , so I check with
netstat -an
tcp46 0 0 *.8080 . LISTEN

Related

Why I can not run server.js on localhost?

So I have a folder with a file server.js, inside server.js there is a code like this:
var http = require ('http');
http.createServer(function(req, res){
console.log(req);
console.log("***********************************");
res.writeHead(200, {'Content-Type' : 'text/html'});
res.write('Hello World');
res.end();
}).listen(80);
also in a folder with this file, I installed node modules and npm and package.json and package-lock.json
but for some reason when I try to run it with node server.json, in doesn't run on port 80 but instead this happens. https://imgur.com/KIgHhBm
How can I fix this? I want to be able to open server.js via chrome, and it should write out hello world

I'm having trouble executing a simple JavaScript code on the server using node JS

I'm a beginner with Node.js.
I have just installed node JS, and I'm trying to create and execute code through a local server (for practice purposes). But it can't be possible to execute the code using localhost:8080 on a web browser nor through the cmd (I'm on windows 7).
Below you can see what I've been trying so far...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World! i did it');
}).listen(8080);
The browser just says that the site can't be reached and the cmd shows nothing after executing the command. So what would be the problem.
It would be a good approach to get the PORT environment variable and to have the server listen on that port, instead of one that is hard-coded.
In node you can get this with: var port = process.env.PORT || 8080
This is saying that if the PORT environment variable is set use that OR use 8080 if it is not set.
Your new code would be:
var http = require('http');
var port = process.env.PORT || 8080;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World! i did it');
}).listen(port);
If you are using Windows CMD, first type 'node'. Now you will be coding directly in node.js. Now, copy and paste your code, and hit enter. Now go, and try the localhost:8080 on your browser.
If you still have the issue, try the same with a different port.
I tested your code on my machine and works perfectly fine.

nodejs web server not responding

i am trying to setup a web server using nodejs. The following code below is directly from the nodejs.org website just configured with my server credentials.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(5000, '10.0.1.51');
console.log('Server running at http://10.0.1.51:5000/');
but when i go to 10.0.1.51:5000 nothing is found i don't even get an error in my console.
also the book i am learning from provided me with this
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and that still doesn't work. I'm not sure on where to look to resolve this issue, thanks.

Trying to make a HelloWorld node.js app

I done everything like say on nodejs.org tutorial for helloworld but I have a big problem. Please see the image below:
IMAGE: http://i.imgur.com/EjAMXcS.png
I save example.js file into D: directory ...
How to start node.js code:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server started');
You run node example.js from your command line (e.g. Windows Power Shell), not from inside the Node.js REPL.
From the command prompt navigate to D drive and then type node example.js to run your code.
When you use some package like morgan you need to install them before using them.Standard way of installing package is(again from command prompt) to navigate to your application folder and type npm install <package-name>. For further information look here:https://www.npmjs.org/doc/cli/npm-install.html

How do I run node.js code after I installed it with xampp?

I run xampp and I was recently told by a friend to try node.js as a replacement for PHP so I thought I should give it a try
I went to node.js's website and installed the .msi file, later I put the following code in a .js file
var http = require('http');
http.createServer(function (req, res) {
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/');
I launched xampp and executed "node app.js" from the command prompt, tried visiting localhost:1337 from my browser but it cannot resolve
What am I doing wrong?

Categories