1 在命令行手工创建了express 4.2的项目,命令行启动成功,浏览器也能访问成功 2 用WebStorm打开此项目,运行它后,在控制台能输出 index.js 中我手工加入的语句 var express = require(‘express’); var router = express.Router();
/* GET home page. */ router.get(’/’, function(req, res) { res.render(‘index’, { title: ‘Express斗地主’ }); });
module.exports = router; console.log(‘hello world’);//手工加的
问题是:通过浏览器无法访问页面,找不到网页。
下面是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’, ‘jade’);
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(’/hello’, 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;
没有更多代码?
这个是主要的代码,还需要什么代码呀?
var router = express().Router();
用npm start启动服务,然后访问localhost:3000,如果你加个app.listen(3000);也可以用node app.js启动服务,然后访问