将nodejs用于localhost时找不到404 404

时间:2015-12-06 14:47:08

标签: javascript node.js

我使用节点和表达4.x创建了一个简单的登录系统。但是我写了localhost:3000并且看到了#34; Not Found 404"。我认为问题是epxress viersion是4.x,但有些代码表示3.x。

这是错误页面:

Not Found

404

Error: Not Found
    at app.use.res.render.message (C:\Users\Administrator\Desktop\front\node\node_web\blog\app.js:34:13)
    at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:330:12)
    at next (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:271:10)
    at cookieParser (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\cookie-parser\index.js:48:5)
    at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:280:7

app.js

var path = require('path');
var express = require('express');
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 settings = require('./settings');
var users = require('./routes/users');
var session = require('express-sessi

on');
var MongoStore = require('connect-mongo')(session);

var app = express();

// view engine setup
app.set('port',process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 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(routes);
// app.use(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: {}
  });
});


app.use(session({
  secret: settings.cookieSecret,
  key: settings.db,//cookie name
  cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
  store: new MongoStore({
    db: settings.db,
    host: settings.host,
    port: settings.port
  })
}));
app.use(express.static(path.join(__dirname,'public')));
routes(app);

module.exports = app;

index.js

module.exports = function(app) {
  app.get('/', function (req, res) {
    res.render('index');
  });
  app.get('/reg', function (req, res) {
    res.render('reg', { title: '注册' });
  });
  app.get('/reg', function (req, res) {
  });
  app.post('/login', function (req, res) {
    res.render('login', { title: '登录' });
  });
  app.post('/login', function (req, res) {
  });
  app.get('/post', function (req, res) {
    res.render('post', { title: '发表' });
  });
  app.post('/post', function (req, res) {
  });
  app.get('/logout', function (req, res) {
  });
};

1 个答案:

答案 0 :(得分:0)

在你的app.js中你正在加载路线,但你并没有告诉快递使用它。将app.use('/', routes);放在404捕获之前。

PS:这是你评论过的话。