我是参考《node.js开发指南》里面自学的,现在在第五章这里,我照着实例express -e ejs microblog创建了一个项目,并且经过折腾后也能正常npm install但是执行项目的app.js时候直接就不行了,也不报错,就是又回去了。 具体可以参考我在这里的提问,问题描述的最后那一段,不知道各位大神碰到过没有,菜鸟小弟谢过了 问题描述
卧槽,居然被我这个菜鸟解决了,但绝对没科学道理,我说一下,我看了用我的环境生成的app.js var express = require(‘express’); var path = require(‘path’); var favicon = require(‘static-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’, ‘ejs’);
app.use(favicon()); app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); 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;
-----------------------------------分割线------------------------------------
然后是书上案例的app.js,是这个样的
/**
- Module dependencies. */
var express = require(‘express’) , routes = require(‘./routes’);
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){ app.set('views’, __dirname + ‘/views’); app.set('view engine’, ‘ejs’); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + ‘/public’)); });
app.configure('development’, function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); });
app.configure('production’, function(){ app.use(express.errorHandler()); });
// Routes
app.get('/’, routes.index);
app.listen(3000); console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
我发现我的app.js少一个“app.listen(3000);”,于是我手动加了一个到我的app.js里,然后就跑起来了,但是我不知道,为什么我的app.js里没有这句话,求大神解释,相关参数如下: 系统:Macosx node版本:v0.10.28 express版本:4.2.0 npm版本:1.4.9