application.js wont start but debug works - node.js

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

Related

NodeJS autorefresh server

Ok, I'm using NetBeans and I have this piece of code:
var express = require("express");
var app = express();
app.get("/", function (req, res) {
res.send("Hi");
});
app.listen(8000);
When I click run file it starts listening on the port 8000. But, when I make a change and then run file again it cannot start listening on port 8000, because the previous one was not terminated. I have the option checked "Apply Code Changed on Save" in Options->HTML/JS->Node.js. How to refresh previously started server instead of starting the new one one?
Ok, the project in NetBeans was set to be run as WebApplication, but not as NodeJS Application and thus it didn't start the nodeJS server. In order to change it you have to right click on your project and go to "Properties"->"Run" and then change "Run As:" to "Node.js Application" and check "Restart running Node.js process".
The 'Apply Code Changed on Save' is imho for debugging. You can run the project instead of file, if you run project and it has been already running, then NetBeans will stop the running project (e.g to free the port) and it will continue and run the projec

process.env variables are undefined even after export

I'm writing a Node.js express app and want to use environment variables for setting the port on which the server should run.
However, I can't seem to get process.env.PORT to read my PORT environment variable.
I've defined the PORT environment variable using export like so:
export PORT=1234 I've also added this line to the ~/.bash_profile file, but process.env.PORT remains undefined.
When I run echo $PORT in terminal, it displays the value (1234) correctly.
I'm running Node V0.12.7 and OSX El Capitan 10.11.1 and really can't find any extra clues on what might be causing this.
Thanks!
EDIT:
Here's the code executed before trying to assign process.env.port to the port variable
var app = require('../app');
var proxy = require("../proxy");
var http = require('http');
var nconf = require('nconf');
nconf.file(__dirname + "/appConf.json");
var appSettings = require('../appSettings');
var port = normalizePort(process.env.PORT || 8080);
There's a great npm package called dotenv, that will auto-load any environment variables from a file called .env in the same directory as your nodeJS application. This could give you a differentPORT variable project to project, instead of globally defining one PORT across all terminals.
As far as everything else, it sounds like PORT should be there (you even said it was properly echo-ing from command line). Did you run node out of the same terminal as you used echo? The only other issues I can think of are if you didn't restart the terminal you're running your server out of after you modified your ~/.bash_profile, or maybe a simple typo somewhere.
I would have posted this as a comment, but I don't have the score for it.
Seems like there is a function or another object named process, it could cover the NodeJS process object.

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 to run express.js 4.0 app in webstorm 7

As you might know that new express.js version has came out and it contains most of the changes including restful routes etc. In previous version to run an app we use to set app.js in webstorm but now in express 4.0 to run an app npm is required npm start is command.
Does any body know how to setup an express 4.0 app in webstrom to run from it?
In your Configuration, go to "JavasScript file" and change it to this value: bin\www
Click the 'Run' button (the green triangle), and in the console you should see: "Express server listening on port 3000". Then you can access your app at http://localhost:3000.
I found one hack for this. That is when you create new express.js 4.0 app you will notice that appname/bin/www.txt file get created which contains following code
var debug = require('debug')('my-application');
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);
});
just comment out this line var app = require('../app'); copy that code and paste at bottom of your app.js i.e appname/app.js
that's it now you run an app from node app.js

http server listening in old port

First i installed the node js with webmatrix and ran a sample node js app. the app was assigned a random port. http://localhost:62369/. After that i installed the express module. As said in their doc. i wrote,
var app = express();
app.get('/',function (req, res) {
res.send('hello world!!');
})
app.listen(3000);
Then i restarted the server. The launched browser was still pointing to http://localhost:62369/ instead of port 3000. Moreover http://localhost:3000/ was not working.
I suggest you to run this code so you can see if you have any problem on saving the code with your IDE:
var app = express(),
port = 4555;
app.get('/',function (req, res) {
res.send('hello world!!');
})
console.log("Server is running on " + port);
app.listen(port);
After that, you need to change the port variable only. It's helpful if you comment what you see after running this code on the console.
make sure that you've saved your code in your file (open it with another editor, maybe something's wrong with your editor), close the command line window and open it again. try to run server. I'm sure the problem is not because of node or express. Try to check everything again.
And also run your server with command line:
cd path/folder
node myFile
I don't know what are you using to run server, but if it's something with UI (in comments you mentioned a click) it can cache your code or something like that. So it's safer to run with commend line.

Resources