总是找不到404

时间:2016-12-23 10:12:37

标签: javascript node.js express

您好我的

中有以下代码

app.js

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');

// Used to connect to the MongoDB database
var mongo = require('mongodb')


var routes = require('./routes/index');

var users = require('./routes/users');

var app = express();


// Define the directory with the views and to use Jade
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


// 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(express.static(path.join(__dirname, 'public')));


// Define what route files to use being routes/index.js for /
// routes/users.js for /users
// The route files then render the page
app.use('/', routes);
app.use('/users', users);
console.log("The Value of Appadadasd",""+ app);

// 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;

这是我的 index.js

var express = require('express');
var router = express.Router();
var mongodb = require('mongodb');



/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/AddServer';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);

    // Get the documents collection
    var collection = db.collection('users');

    //Create some users
    var user1 = {name: 'modulus admin', age: 42, roles: ['admin', 'moderator', 'user']};
    var user2 = {name: 'modulus user', age: 22, roles: ['user']};
    var user3 = {name: 'modulus super admin', age: 92, roles: ['super-admin', 'admin', 'moderator', 'user']};

    // Insert some users
    collection.insert([user1, user2, user3], function (err, result) {
      if (err) {
        console.log(err);
      } else {
        console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result);
      }
      //Close connection
      db.close();
    });
  }
});
module.exports = router;

和我的users.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/users', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

当我输入http://localhost:3000/时,它完美地工作 但当我试图打电话给http://localhost:3000/users给我时

未找到

404

Error: Not Found
    at D:\AddServer\AddServer\app.js:45:13
    at Layer.handle [as handle_request] (D:\AddServer\AddServer\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\AddServer\AddServer\node_modules\express\lib\router\index.js:312:13)
    at D:\AddServer\AddServer\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (D:\AddServer\AddServer\node_modules\express\lib\router\index.js:330:12)
    at next (D:\AddServer\AddServer\node_modules\express\lib\router\index.js:271:10)
    at D:\AddServer\AddServer\node_modules\express\lib\router\index.js:618:15
    at next (D:\AddServer\AddServer\node_modules\express\lib\router\index.js:256:14)
    at Function.handle (D:\AddServer\AddServer\node_modules\express\lib\router\index.js:176:3)
    at router (D:\AddServer\AddServer\node_modules\express\lib\router\index.js:46:12) 

它没有路由到users.js我认为我正在做一些愚蠢的错误,但我是新节点无法弄明白。

1 个答案:

答案 0 :(得分:3)

您正在通过定义

users处加载/users个路由
app.use('/users', users);

但是,在您的users.js文件中,您只定义了/users路由,http://localhost:3000/users/users可以访问(由于挂载),而不是http://localhost:3000/users

尝试将path中的/users路由更改为/

{/ 1}}

e.g。

users.js

修改

在讨论确切要求后,您可以进行以下更改以访问router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); });

<强> app.js

http://localhost:3000/user

<强> users.js

app.use('/user', users);