Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Express.js Using app.js or 'npm start'?

I've recently installed Node.js on a local server and when I create a 'server.js' file (adding a server using the .createServer() method), it loads fine.

However after installing Express.js, the default files are as follows:

/bin
/node_modules
/public
/routes
/views
app.js
package.json

After following some documentation, I am instructed to go to Terminal and enter the following command:

node app.js

To which nothing happens, the command line refreshes to the next line in less than a second, and opening a browser after visiting the proper IP and port, to no avail.

Below is the code inside of the app.js file:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
        message: err.message,
        error: {}
      });
    });

module.exports = app;

I understand that the actual 'express' module that is being required in the beginning of the file is where the magic happens, however, when I run the command line:

node app.js

Nothing happens. However, if I call the following line:

npm start

Everything appears to be okay. I would like to follow the documentation as is, any reason why the app.js wouldn't be working?

Thank you.

UPDATE: My question was too similar to another one already posted, so I must clarify exactly how they were different. In the other question, a person was questioning a code number they received while running the Supervisor command on the default 'app.js' file.

While similar in nature, this question should remain, as those who are confused by using my same approach will focus on the identity of the actual 'app.js' file by using 'node app.js' without having full knowledge of the Supervisor utility.

Regards.

like image 462
Mindsect Team Avatar asked Sep 24 '15 23:09

Mindsect Team


2 Answers

Thank you all for your great responses, as they really allowed me to understand what is actually going on with the app.js file and where it receives it's functionality. Thank you to both Matthew Bakaitis and Bjarni Leifsson for their great input.

The only reason why I am going to go ahead and answer my own question is because while the nature of the app.js file was explained, exactly how to replicate calling the 'node app.js' command from the command line as to replicate a Node.js book that I was following wasn't implicitly addressed.

After searching google with the specific phrase "app.js in previous express.js versions", I happened upon a great article by Jilles Soeters entitled "Understanding the Express app.js":

http://jilles.me/getting-the-express-app-js/

Below is the excerpt of the solution that worked for me:

The file I'm covering is app.js, the main configuration file for your Express app. When I first opened app.js it confused me. I will save you the trouble of doing research and just cover them here.

Before you do anything add the following to your app.js

app.listen(3000);

You need that in order to be able to actual open your app in the browser. Go to 127.0.0.1:3000 after you've started your app (using node app.js)

After doing this, I was able to run the command

node app.js

I was able to run this command from the root directory of the Express install and proceed with my Node.js book with no additional problems.

like image 108
Mindsect Team Avatar answered Sep 28 '22 10:09

Mindsect Team


This is a common problem that is caused when tutorials don't clearly explain what express is doing when it generates an app. You're trying to learn the new tech, but the tutorial is actively working against you. :(

The answer:

When you use the generator, package.json is configured so that npm start calls ./bin/www.

That file includes app.js and after the include, calls app.listen.

app.js doesn't call app.listen which is why if you call it directly, it exits with no code or info. You've got to call ./bin/www or you have to modify app.js...which then defeats some of the reasons you'd use a generator.

A related question here on the site saw a similar problem when trying to use supervisor to keep an app running but kept getting an exit 0 result.

like image 25
Matthew Bakaitis Avatar answered Sep 28 '22 11:09

Matthew Bakaitis