2017-07-31 105 views
0

下面的代碼很凌亂,所以不要太多評價!我試圖將一個基本的用戶配置文件發佈到我的數據庫中,我不認爲我離我很遠,但我一直得到一個404.如何使用node.JS將數據發佈到MongoDB數據庫?

我很熟悉所有這些技術,所以有人請賜教我我做錯了。

Node.js的POST方法

var express = require('express'); 
var router = express.Router(); 
var mongo = require('mongodb'); 
var assert = require('assert'); 
var url = 'mongodb://localhost:27017/local'; 

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

router.get('/getData', function(req, res, next){ 
    mongo.connect(url, function (err, db) { 

     assert.equal(null, error); 
     var cursor = db.collection('userData').find(); 
     cursor.forEach(function(doc, err){ 
      assert.equal(null, err); 
      resultArray.push(doc); 
     }, function() { 
      db.close(); 
      res.render('index', {items: resultArray}); 
     }); 
    }); 
}); 

router.post ('/insert', function(req,res,next) { 
    var item = { 
     email: req.body.email, 
     firstName: req.body.firstName, 
     lastName: req.body.lastName, 
     password: req.body.password 
    }; 
    mongo.connect(url, function (err, db) { 
     assert.equal(null, err); 
     db.collection('userData').insertOne(item, function (err, result) { 
      assert.equal(null, err); 
      console.log('item has been inserted'); 
      db.close; 
     }); 
    }); 

    res.redirect('/'); 
}); 
module.exports = router; 

形式HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 


    <title>SignIn Page</title> 
    <link rel="stylesheet" type="text/css" href="/stylesheets/signIn.css"/> 


</head> 

<body> 

<div class="background"> 


<div class="loginFormWrapper"> 
    <form action="https://stackoverflow.com/users/submit" method="POST"> 
     <div class="loginForm"> 
      <label for="firstName">First name:</label> 
      <input type="text" class="form-control" id="firstName" name="firstName" placeholder="first name"> 

      <label for="lastName">Last name:</label> 
      <input type="text" class="form-control" id="lastName" name="lastName" placeholder="last name"> 

      <label for="email">Email address:</label> 
      <input type="email" class="form-control" name="email" id="email" placeholder="email"> 

     </div> 
     <div class="form-group"> 
      <label for="pwd">Password:</label> 
      <input type="password" class="form-control" name="password" id="password" placeholder="password"> 
     </div> 
     <div class="checkbox"> 
      <label><input type="checkbox"> Remember me</label> 
     </div> 
     <button type="submit" class="btn btn-default">Submit</button> 

    </form> 
    <form action="users" method="GET"> 
     <button type="submit" class="btn btn-default">get result</button> 
    </form> 
</div> 

</div> 


</body> 
</html> 

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'); 
    var validate = require('form-validate'); 
    var mongoose = require('mongoose'); 




    var index = require('./routes/index'); 
    var users = require('./routes/users'); 
    var About = require('./routes/about'); 
    var signIn = require('./routes/signIn'); 
    var contact = require('./routes/contact'); 


    var app = express(); 




    // view engine setup 
    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: true })); 
    app.use(bodyParser.json()); 
    app.use(cookieParser()); 
    app.use(express.static(path.join(__dirname, 'public'))); 
    app.use('/', index); 
    app.use('/users', users); 
    app.use('/About', About); 
    app.use('/signIn', signIn); 
    // app.use('/contact', contact); 







//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 handler 
app.use(function(err, req, res, next) { 
    // set locals, only providing error in development 
    res.locals.message = err.message; 
    res.locals.error = req.app.get('env') === 'development' ? err : {}; 

    // render the error page 
    res.status(err.status || 500); 
    res.render('error'); 
}); 

module.exports = app; 

user.js的

var express = require('express'); 
var app = require("mongoose"); 
var router = express.Router(); 


/* GET users listing. */ 
router.get('/', function(req, res, next) { 
    res.send('respond with a resource'); 
}); 

module.exports = router; 
+0

你要求什麼網址? –

+0

var url?這是mongodb的默認設置。在後,並獲取方法,我通過網址連接到該數據庫....我的代碼是不同的教程的合併,所以一些var名稱可能是奇怪的! – lc1993

+0

我的意思是像localhost:3000/users /這種類型的URL。你說你得到404,但你沒有提到的URL地址 –

回答

1

您已發佈到網址users/submit,但我沒有看到任何API爲users/submit。您曾經說過要使用/users網址的用戶,但是您是否已經在/users內定義了/submit

你可以通過這個 Routing in expressjs

+0

虐待有一個閱讀,謝謝。我認爲它很明顯,我不明白路由很好 – lc1993