Why does NodeJS download the file instead of holding the web - node.js

I am not sure about why there is a difference between Mac OS & Windows.
The problem is, on Windows environment, the Chrome/Edge downloads the NodeJS response instead of just loading the webpage. Whenever I visit "localhost:8081" on Chrome/Edge, it happens again and again. I would like to do it as what MAC OS does, anyone can save me?!
Windows Side Response
Windows
On MacOS, it does sticking on the browser window.
Any idea for this issue?
Forgot to mention NodeJs version, MACOS: v10.15.3, WINDOWS: v11.12.0
Hopefully, the version doesn't matter in this case. :'(
MacOS
Thanks!
var http = require('http');
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('HelloWorld');
}).listen(8081);
console.log('server running at http://127.0.0.1:8081/');

Related

Node.js not working on Windows XP machine

I am just getting started with node.js and I have followed multiple tutorials to get it working on my Windows XP machine.
Downloaded the msi and installed with no issues, opened a command prompt and typed node -v and nothing displays. I created a folder and made a JS file with console.log('hello'); in it, changed to that directory and typed node hello.js and nothing shows.
I then added this code to the hello.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/');
and ran the file from the command line again, it paused briefly and then again displayed nothing.
Been through 10 different tutorials on 10 different sites and can't find a thing to answer this question, even all the information here was no help at this point.
I have also checked the path and it is fine and rebooted the machine just in case.
Seriously stuck!
You need to set your path to node (Environment Variable). Check the following address:
http://www.hacksparrow.com/install-node-js-and-npm-on-windows.html
This is necessary for running node from any folder in the system.
Or you can add node as an enviroment variable in Windows XP's settings:
https://support.microsoft.com/en-us/kb/310519

How to access to the application through browser?

How can I access my server through the browser once I've set up the server ?
In PHP I simply have to put my files in the www/ folder and then go to http://127.0.0.1/index.php, but how can I do that with NodeJS ?
My server's code is from a tutorial :
var app = require(‘express’).createServer();
app.get(‘/’, function(request, response){
response.send(‘Hello Web Designer’);
});
app.listen(8888);
But whenever I go to http://127.0.0.1:8888/ i get a "Problem loading the page". So either my server is not running correctly (hard to tell when the NodeJS console shows "...") or I am not accessing it correctly.
What can I do please ?
you have to send a http head and end the response.
Try this code.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello world');
res.end();
}).listen(8888);
(Pseudo Code)
Also don't use any frameworks to learn how node works.
You can run you index.js now and call localhost:8888
Did you start the server? The apache server runs php, in the nodejs world, there is a javascript runtime that needs to be started.
Try node server.js from the command line (or whatever your file is called) to get it to listen and serve incoming requests.

Node.js console.log() not logging anything

Trying out node.js for the first time. Set up node, set up the example app from the nodejs.org site. Can start the server fine, but console.log() isn't actually logging anything. Tried the Javascript console in Chrome, Firefox, and Safari - nothing appears in the log. Also checked Console on my Mac just for kicks, nothing was there either. What am I missing?
(Here's the example code that works but doesn't log anything.)
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/');
In a node.js server console.log outputs to the terminal window, not to the browser's console window.
How are you running your server? You should see the output directly after you start it.
This can be confusing for anyone using nodejs for the first time. It is actually possible to pipe your node console output to the browser console. Take a look at connect-browser-logger on github
UPDATE: As pointed out by Yan, connect-browser-logger appears to be defunct. I would recommend NodeMonkey as detailed here : Output to Chrome console from Node.js
Using modern --inspect with node the console.log is captured and relayed to the browser.
node --inspect myApp.js
or to capture early logging --inspect-brk can be used to stop the program on the first line of the first module...
node --inspect-brk myApp.js

node.js segmentation fault:11

having a problem getting node.js to run. I'm on OSX Lion, followed the installation instructions for node. I'm on v0.5.9-pre, according to node --version, but when I try to run
node app.js
I get "Segmentation fault: 11".
The app.js file is just the hello world example from the main site:
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/');
Nothing special. Tried looking over the web, but nothing seems to answer this. Any ideas?
I had this after upgrading from Node 0.4 to 0.6. I just needed to delete the node_modules directory from my app and re-install the dependencies with 'npm install' again.
Never mind. Just used homebrew to install node.js/npm and all working fine now. Thanks anyway.

listen() doesn't return?

I'm trying to get started with Node, and have hit a hump. When I try
to run the below (same example I've seen everywhere) the "starting..."
line executes, as well as "created.", but it seems like the script gets stuck on the next line, and never prints "started." (and the server doesn't work, tried via a
browser, telnet, curl, etc). Any advice on how to debug?
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
console.log("starting...");
var s = http.createServer(onRequest);
console.log("created.");
s.listen(8888);
console.log("started.");
Debian GNU/Linux 6.0.1 (squeeze), 2.6.39-x86_64
Compiled node.js 2011.07.19 v0.4.10 (stable) from source.
Thanks!
Actually it looks like listening has some problems with socket access. Try to change port number, turn off the firewall or run node with root access. Maybe it should help;
In other way try it with python or smth like that;
Change the port number to 3000 AND run as root. Ensure port 3000 is NOT being used (i.e. use netstat). You should see the "started" message. Otherwise, I can't really help. I'm running Node 0.4.10 on Ubuntu and I copied/pasted your code; it runs perfectly. If it still doesn't work, reinstall Node.

Resources