node.js错误:未找到

时间:2015-06-21 16:31:25

标签: node.js mongodb monk

我是node.js初学者。 我正在尝试为用户创建一个页面,将数据输入MongoDB。 我在网上看了一个教程,但下面的错误突然出现了。 已经仔细检查过我的代码是完全一样的。

我是否必须安装别的东西?

错误:未找到

at /.../testproject/app.js:44:13
at Layer.handle [as handle_request] (/.../testproject/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/.../testproject/node_modules/express/lib/router/index.js:302:13)
at /.../testproject/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/.../testproject/node_modules/express/lib/router/index.js:321:12)
at next (/.../testproject/node_modules/express/lib/router/index.js:261:10)
at /.../testproject/node_modules/express/lib/router/index.js:603:15
at next (/.../testproject/node_modules/express/lib/router/index.js:246:14)
at Function.proto.handle (/.../testproject/node_modules/express/lib/router/index.js:166:3)
at router (/.../testproject/node_modules/express/lib/router/index.js:35:12)

这是我的index.js

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

router.get('/', function(req, res, next) {
           res.render('index', { title: 'New Title' });
           });



router.get('/userlist', function(req, res) {
   var db = req.db;
   var collection = db.get('usercollection');
   collection.find({},{},function(e,docs){

    var name = [];
    var objKey = Object.keys(docs);
    objKey.forEach(function(objectid){
      var itemkeys = Object.keys(docs[objectid]);
      itemkeys.forEach(function(itemkey) {
        var itemvalue =docs[objectid][itemkey];
        console.log(objectid+': '+itemkey+' = '+itemvalue);
        if (itemkey == "username") {
          name.push(itemvalue);
        }
      })
    })
       console.log(name);
       res.render('userlist', {
          "userlist" : name
       });
    });
});


/* POST to Add User Service */
router.post('/adduser', function(req, res) {

    // Set our internal DB variable
    var db = req.db;

    // Get our form values. These rely on the "name" attributes
    var userName = req.body.username;
    var Email = req.body.email;

    // Set our collection
    var collection = db.get('usercollection');

    // Submit to the DB
    collection.insert({
        "username" : userName,
        "email" : Email
    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem adding the information to the database.");
        }
        else {
            // If it worked, set the header so the address bar doesn't still say /adduser
            res.location("userlist");
            // And forward to success page
            res.redirect("userlist");
        }
    });
});

/* GET home page. */
router.get('/newuser', function(req, res) {
           res.render('newuser', { title: 'Add New User' });
           });

module.exports = router;

以及来自app.js 第44行

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

1 个答案:

答案 0 :(得分:1)

http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 提前搜索它所说的“继续并提交。享受404错误。我们即将修复它。”

您刚看到教程作者说您会看到的404错误。按照教程中的说法进行操作,大概你会在路由器中添加必要的POST代码。

就像我上面说的那样,你想访问/ newuser页面以便输入新用户。