我的应用程序文件夹结构是这样的。
app/ config/ app.js env.js server.js
每当我开始运行我的 app.js 文件时,它都会让我的服务器以未定义的方式启动。这是代码的要点。
server.js
// Module dependencies.
var http = require('http'),
app = require('./config/app')();
// Start server
app.listen(app.get('port'), function(){
console.log('App is Up at '+ app.get('port') + ' as ' + process.env.NODE_ENV);
});
config/app.js
var express = require('express'),
path = require('path'),
ejs = require('ejs');
module.exports = function() {
var app = express();
var publicDir = path.join(__dirname, '../public');
// Store all environment variables
app.set('port', process.env.PORT || 3000);
// Basic configuration
app.configure(function() {
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(publicDir));
});
// Environment specific configuration
require('./env')(app);
return app;
};
config/env.js
var express = require('express'),
hbsPrecompiler = require('handlebars-precompiler'),
path = require('path');
module.exports = function(app) {
// development compile Handlebars and show errors
app.configure('development', function(){
app.set('db-uri', process.env.MONGOLAB_URI || 'mongodb://localhost/App');
hbsPrecompiler.watchDir(
path.join(__dirname, "../public/templates"),
path.join(__dirname, "../public/templates/compiled/templates.js"),
['handlebars', 'hbs']
);
app.use(express.errorHandler());
});
app.configure('production', function(){
app.set('db-uri', process.env.MONGOLAB_URI);
});
};
节点服务器.js
输出
/usr/bin/node app.js
App is Up at 3000 as undefined