I have followed a few tutorials on how to get this setup thinking I wasn't getting it but no matter what I try I can't get the server to run.
In it's basic form I just used the express-generator node package to run
express backend
and I get all the files that it looks like I should including an app.js (by the way I'm using the latest stable versions as installed yesterday) when I try to run
node app
node app.js
or
node ./bin/www
(I've seen that somewhere else as well) I can't get the server to run. Running the first two just returns the console and nothing happens, running .bin/www looks like it tries to run something but it just sits and nothing happens.
in bin/www I see the actual script to run the server with the port and all, so I'm assuming that's where I need to be looking.
If you need it for reference this is the contents of app.js generated by the express generator
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(__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;
And the bin/www file contains this
#!/usr/bin/env node
var debug = require('debug')('backend');
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);
});
UPDATE: I have discovered that running
npm start
is actually starting the server, and going to localhost:3000 shows the default express server running. I was expecting an output per the many tutorials that I was reading (and yes I made sure to find ones on express v4)
You just cant run an app after generated with express generate follow the following steps in order to run an express app.
// Generate the express app
express backend
// Change into backend directory
cd backend/
// Install the required dependencies
npm install
// Debug and run the server
DEBUG=backend ./bin/www
Tested