Node.js just returns "..." in terminal - node.js

I'm a front end dev trying to learn and get into Node.js
Having trouble at the first hurdle. I have an app.js file in the root of Mac: "/"
app.js has the following:
console.log('hello world!');
Whenever I run node app.js in the terminal I get the following:
> node app.js
...

It looks like you're running the Node.js REPL first. Don't do that... the Node.js REPL is for running immediate evaluations.
Just try running node app.js from your normal system terminal.

Related

Nodejs waiting for 127.0.0.1:9229 to be free

When I try to use debugger in node to open debugger, I get an error 'Timeout (2000) waiting for 127.0.0.1:9229 to be free'. How can I resolve this and run the debugger correctly ?
function foo() {
var a = 5;
debugger
console.log(a)
}
foo()
I have already tried changing the port using node inspect --port=9230 app.js and it doesn't work.
Try this:
node --inspect-brk app.js
replace app.js with your file name that you want to run, and you can insert your additional command alongside with this line.
I had the same problem, it took me hours to figure it out...
Run the following command:
node inspect --port=9228 file.js
I had the same issue by using VS Code. VS code document helps. Replace program.js with your js file name.
https://code.visualstudio.com/docs/nodejs/nodejs-debugging
if the program should not start running but must wait for the debugger to attach:
node --inspect-brk program.js
Use node --inspect-brk {js file name} instead. It will work.
--inspect-brk=[host:port]
Enable inspector agent
2.Bind to address or hostname host (default:127.0.0.1)
3.Listen on port port (default: 9229)
4.Break before user code starts
I think the issue here is the command you give to node, it should be node --inspect..
You are missing the -- in front of inspect :)
Install
npm install --global node-inspect
Then
node-inspect script.js
Ref: https://www.npmjs.com/package/node-inspect

node command vs app.listen()

I'm confused. So if I use gulp-develop-server, it's got a app.listen():
gulpfile.js
config.server.path is set to './app,js'
server = require('gulp-develop-server');
gulp.task('default', ['server:start'], function(){
});
gulp.task('server:start', function() {
server.listen({ path: config.server.path});
});
My app.js has this:
var koa = require('koa')();
koa.listen(config.server.port, function(){
console.log('Koa app is listening on port ' + config.server.port);
});
so I'm trying to understand better how node is being started. I see people mention doing it manually like "node app.js". So doesn't koa.listen() automatically do a "node" command to start the koa web server? If I use gulp-develop-server and specify server.listen, isn't that doing 2 server.listen() for node?
Just trying to understand the basics here and can't understand why anyone would manually type in 'node [file with .listen]' manually. I'm not doing that manually and server.listen() obviously uses the 'node' command on my app.js.
Your gulpfile is a Node script. So when you run gulp server:start you're executing a Node application, the gulp command is essentially node plus some extra functionality.
The way gulp-develop-server works is it runs an additional Node application as a child process. server.listen is basically just telling gulp-develop-server what script to run.
The naming is a little confusing, but essentially what's going on is: You have 2 Node applications running on your machine (one that you can see, and one in the background), but only 1 server.

How can I run grunt as a daemon?

I am running a packaged nodejs webserver that allows for reading of epub files (Readium-JS), and it is started with the grunt command.
However, if I run this on my VPS the server dies as soon as my terminal connection ends.
How can I run this task as a daemon?
I have looked at options like grunt-forever and grunt-daemon but the way the Gruntfile is written using load-grunt-config is messing with my mind and I can't piece together how to isolate the server code.
Here's the solution I found:
As was suggested above, using pm2
However, when I ran
pm2 start grunt
I got an error saying that the grunt module did not exist, which was weird.
So I ended up writing a script which worked:
-- start.js --
var pm2 = require('pm2');
pm2.connect(function() {
pm2.start({
script : '/usr/local/bin/grunt', // Script to be run
args: '--force',
}, function(err, apps) {
pm2.disconnect();
});
});
After running node start.js from the command line, everything sailed smoothly.

application.js wont start but debug works

I am new to node.js and I want to get webserver running but I got this problem where trying to debug application by running node ./bin/www works perfectly but trying to launch it by node app.js dosen't do anything.
When i type out node app.js in terminal blank line appears like its loading something and dissapears in few seccods without any error or starting application.
The problem is that app.js is exporting an app object, not starting a server. If you look at the code for bin/www you'll see that it loads the app object and uses it to start a server.
#!/usr/bin/env node
var debug = require('debug')('tmp');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
You need to either start the app using bin/www, or modify and move that code to the end of app.js if you don't want the separate start script.
If you want to see the debug messages, you need to add the DEBUG environment variable to your shell session. For development environments, you could do this automatically by placing it in your .bashrc file.
export DEBUG=express:*
Or if you do not want to do that, you can just do this:
DEBUG=express:* node ./bin/www
http://expressjs.com/guide.html#debugging-express

node.exe does not read file, but returns ellipsis (...)

End feb 2014 I downloaded node to c:\dev\0.10\ of my windows 7 machine, and node.exe opens fine.
Inspired by Smashing Node (some book), I want to achieve the following:
Firefox shows the plain text Smashing Node! if i point it to localhost:3000 and run in the node console
node my-web-server.js
where my-web-server.js file next to node.exe contains
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
but I fail: browser says
cannot connect with webserver on localhost:3000.
If paste the above oneliner in node it reacts with
{ domain: null,
_events: ..etc... }
Ignoring that, browser F5 results in Smashing Node!.
Node refuses the simplest of files, say have a file called hello.js next to node.exe and file contains the ascii text
console.log("hello");
i type:
node hello.js
node returns
...
(an ellipsis in dark gray)
Expected was: node returns hello
i type a file that does not exist like this:
node die
node returns
...
if i type
var http = require('http');
node:
undefined
(in a darkish gray) Expected was: something like ok, especially since the above oneliner resulted in a web server.
If however i type
npm install colors
node reacts with
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
of course
node --version
also responds with an ellipsis.
What can i do?
How do i make node to process files?
You need to run node hello.js (and npm) on your command line shell (e.g. cmd.com or Windows PowerShell).
You are trying to run it in the Node REPL (node console), where you are expected to type only JavaScript.
It's hard to tell where the Smashing Node failed, particularly as a one-liner. The code runs OK on my machine, but you can split the code as follow and add a few console.log() to see how is executing:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
Save this code as my-web-server.js as you did before and then run from the command line "node my-web-server.js" and point your browser to localhost:3000
Also, I've got a very basic Node.js tutorial that might help you with the basics: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

Resources