2017-06-17 76 views
1

我是Nodejs和mongodb的新手,嘗試將數據插入數據庫並在提交表單時獲取404。不知道代碼中出了什麼問題。任何幫助,將不勝感激。這是我的代碼如下。Nodejs + Expressjs + EJS + Mongodb

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 mongo = require('mongodb'); 

var index = require('./routes/index'); 
var restaurant = require('./routes/restaurant'); 
var shopping = require('./routes/shopping'); 
var hotel = require('./routes/hotel'); 
var aboutus = require('./routes/about'); 
var event = require('./routes/event'); 
var admin = require('./routes/admin'); 



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: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.use('/', index); 
app.use('/restaurant',restaurant); 
app.use('/shop',shopping); 
app.use('/hotel',hotel); 
app.use('/about',aboutus); 
app.use('/event',event); 
app.use('/admin',admin); 
app.use('/insert',admin); 
app.use('/get-data',admin); 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Page 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; 

admin.js

var express = require('express'); 
var router = express.Router(); 
var mongo = require('mongodb'); 
var mongoClient = mongo.MongoClient; 
var assert = require('assert'); 

var url = 'mongodb://localhost:27017/aligarhcity'; 

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

//READ THE DATA FROM DATABASE 
router.get('/get-data', function(req,res,next){ 
    var resultArray = []; 
    mongoClient.connect(url,function (err,db) { 
    assert.equal(null,err); 
    var cursor = db.collection('shopping').find(); 
    cursor.forEach(function(doc,err){ 
     assert.equal(null,err); 
     resultArray.push(doc); 
    },function() { 
     db.close(); 
     res.render('/admin',{item:resultArray}); 
    }); 
}); 
}); 

//iNSERT THE DATA IN THE DATABASE 
router.post('/insert', function(req,res,next){ 
    var shop = { 
    shop_name:req.body.shop_name, 
    shop_address:req.body.shop_address, 
    shop_city:req.body.shop_city, 
    shop_zip:req.body.shop_zip, 
    shop_email:req.body.shop_email, 
    shop_phone:req.body.shop_phone, 
    shop_rating_5: '10', 
    shop_rating_4: '10', 
    shop_rating_3: '10', 
    shop_rating_2: '10', 
    shop_rating_1: '10' 
}; 
console.log(shop_name,shop_city,shop_address,shop_zip,shop_email,shop_phone); 
mongoClient.connect(url,function (err,db) { 
    assert.equal(null,err); 
    db.collection('shopping').insertOne(shop,function (err, result) { 
     assert.equal(null,err); 
     console.log('Item Inserted Successfully'); 
     db.close(); 
    }); 
}); 
res.redirect('/'); 
}); 

module.exports = router; 

Admin.ejs

<!DOCTYPE html> 
<html> 
<head> 
    <title><%=title%></title> 
    <link rel='stylesheet' href='/stylesheets/style.css' /> 
</head> 
<body bgcolor="White" height="100%"> 
<table height="100%" width="100%" border=0> 
    <tr width="100%"> 
     <td width="100%" height="100%"> 
      <table width="100%" height="100%" background="/images/city.gif" border=0> 
       <tr width="100%" height="100%"> 
        <td width="100%" height="100%"> 
         <% include template/header.ejs%> 
         <h1 align="center"><img src="/images/aligarh.png" height="450" width="1000"></h1> 
        </td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
    <tr width="100%"> 
     <td width="100%"> 
      <div> 
      <form action="/insert" method="post"> 
       <table width="30%" border="1"> 
        <tr> 
         <td> 
          <label for="shop_name">Shop Name:</label> 
         </td> 
         <td align="left"> 
          <input type="text" id="shop_name" name="shop_name" placeholder="Enter your shop name" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label for="shop_address">Shop Address</label> 
         </td> 
         <td align="left"> 
          <input type="text" id="shop_address" name="shop_address" placeholder="Enter your shop address" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label for="shop_city">Shop City</label> 
         </td> 
         <td align="left"> 
          <input type="text" id="shop_city" name="shop_city" placeholder="Enter your shop city" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label for="shop_zip">Shop Zip</label> 
         </td> 
         <td align="left"> 
          <input type="text" id="shop_zip" name="shop_zip" placeholder="Enter your shop zip" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label for="shop_email">Shop Email</label> 
         </td> 
         <td align="left"> 
          <input type="text" id="shop_email" name="shop_email" placeholder="Enter your shop email" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label for="shop_phone">Shop Phone</label> 
         </td> 
         <td align="left"> 
          <input type="text" id="shop_phone" name="shop_phone" placeholder="Enter your shop phone" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <input type="submit" value="Submit"/> 
         </td> 
        </tr> 
       </table> 
      </form> 
      </div> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <table border="1"> 
       <tr> 
        <td> 
         <a href="/get-data"> Load Data</a> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <% 
         var item=[]; 
         item.forEach(function (items) { %> 
         <li> 
          <%=items.shop_name%> <br> 
          <%=items.shop_address%> <br> 
          <%=items.shop_zip%> 
         </li> 

         <% }) 
         %> 
        </td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
</table> 
</body> 
</html> 
+0

路由問題 - 檢查路由上的請求方法(post或get)。 –

+0

你可以看看代碼並解釋 – May

+0

你正在提交表單的URL嗎? –

回答

0

404主要由服務器,請檢查您的快件實現,路線你試圖訪問是否在代碼中執行

+0

你可以看看代碼並解釋 – May

+0

給定的代碼是不足以檢查你的問題。根據代碼說只允許以下路線 'app.use('/',index); app.use('/ restaurant',restaurant); app.use('/ shop',shopping); app.use('/ hotel',hotel); ('/ about',aboutus); app.use('/ event',event); app.use('/ admin',admin); app.use('/ insert',admin); app.use('/ get-data',admin);' 但是,爲了驗證整個流程,這段代碼是不夠的。我不知道在其他一些子模塊拋出錯誤或不是 需要檢查完整源代碼 – Krishna

+0

我只是使用問題的管理員鏈接。休息一切正常。從我的根頁面我點擊管理鏈接,然後admin.ejs被加載。後加載我只是在窗體中添加細節,並在提交按鈕上按住,然後按照窗體中的動作標籤插入/插入,並給我404。在我的app.js中,我將/ insert再次路由到管理頁面 – May