Why I can not run server.js on localhost? - javascript

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

Related

Node JS server not reflecting changes in server file

Iam new to Node JS when i make any changes to the node file it is not updating in the server and also when i change the port number and kill the server and start it again it is not working
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello');
}).listen(8080);
I have found a quick solution to fix this thing. You have to install a node module called as nodemon. This module helps to automatically restart your Server once you made the changes in Source code.
npm install -g nodemon
And nodemon will be installed globally to your system path.
You can also install nodemon as a development dependency:
npm install --save-dev nodemon
By default, node does not automatically restart when there are any changes in the file.
You might want to try the package nodemon, which can watch for file changes and auto restart the server.
You can try adding an host name to your listen function
let 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');

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

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

Using Node to open index.js throws error

I have not used Node before, but have setup an Apache localhost.
In my directory, I have index.js which reads
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
and a package.json which requires Express.js.
In the command line, I cd into the workspace and run node index.js. But it does not open the server and instead throws this error.
What needs to be done to produce a local server listening on port 3000?
Node version is 6.7.0, express is 4.15.2. Running on Windows 10.
It looks like your text editor may be inserting some unsupported (non-UTF8) BOM in your index.js. Try adjusting your text editor preferences/settings to either instead include a UTF-8 BOM or remove BOMs entirely (node will read the javascript file as UTF-8 no matter what).

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