Node.js console.log() not logging anything - node.js

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

Related

Why does NodeJS download the file instead of holding the web

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/');

Do not know how to start debugging a Node.js code in JetBrains WebStorm

I am trying to learn to debug a Node.js code in WebStorm. The code is the simple "Getting Started" code from here:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
The above code works correctly after I ran node app.js at command line and visited http://localhost:3000. Next I tried to debug it locally in JetBrains WebStorm following instructions here. This is the debug configuration of Node.js application in WebStorm:
I clicked the bug icon to run the Node.js code under debugging mode. This is the output:
"C:\Program Files (x86)\JetBrains\WebStorm 10.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=24163 --nolazy --inspect app.js
Debugger listening on ws://127.0.0.1:24163/bffa51bd-74bd-4257-8174-2c8ab3768e17
For help see https://nodejs.org/en/docs/inspector
I guess that it is equivalent to running the "node app.js" command. The instruction says I need to "Perform the steps that will trigger the execution of the code with the breakpoints." I guess that means I open the URL "http://localhost:3000" in browser to send a request to the web server that the app.js is implementing, and I did it. I have set up breakpoints on every line of the source app.js and I expected that the breakpoints should be hit somewhere in the source, but the browser reported an "Unable to connect" error and nothing happened in WebStorm -- no breakpoint is hit. I have checked the instructions but could not figure out what I am missing. Could you please help me with this problem? If you need more information please let me know. Thanks a lot!
PS: app.js is in the ...\test folder.
==Edit:==
PS2: The browser can print "Hello world" just because "node app.js" is still running. After I terminated it, the browser reports "Unable to connect". So that means the URL does not know how to connect to the webserver launched by WebStorm debugger. Is the port wrong?
PS3: This is what returns from http://localhost:31496/json/list (the port number changes in every debugging session). Hope it can help troubleshooting.
description "node.js instance"
devtoolsFrontendUrl "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"
faviconUrl "https://nodejs.org/static/favicon.ico"
id "0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"
title "app.js"
type "node"
url "file://E:..._test_app.js"
webSocketDebuggerUrl "ws://127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"
As far as I can see, you are using webStorm 10. It's quite old and doesn't support new debugger protocol introduced in recent Node.js updates.
Please downgrade Node.js to v. 4.x (at least) or, better, upgrade Webstorm to 2017.3

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.

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

Why does Cloud9 IDE return the error "Script does not exist" when trying to run simple Node.js script?

I am attempting to create a simple Hello World Node.js script using Cloud9 IDE. My code is below and is the atypical 6 lines:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT);
I don't see any errors and the IDE is certainly not alerting me to any. I've ensured that the file is saved (even going so far as to commit it to my repo). Unfortunately, whenever I try to run the above code I receive the following error:
Error on server
Received following error from server:
"Script does not exist: server.js"
To illustrate the run configuration I've included the below screenshot:
I am pretty sure I am missing something obvious, but whatever it is its alluding me. What do I have misconfigured that could cause my server.js file to not run?
Your configuration is correct, there is however a temporarily problem that causes some of the VM's to not run a file. I've seen it before and it usually goes away after some time. Quick fixes now:
Either wait
Create a new project with the same file, chances are that it'll run
Your filepath doesn't look right. Shouldn't it be server.js and not /server.js? Try creating a new run configuration and set the file path to server.js.

Resources