我是Nodejs和mongodb的新手,并尝试将数据插入数据库并在提交表单时获取404。不确定代码中有什么问题。任何帮助,将不胜感激。这是我的代码如下。
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 :(得分:0)
404主要来自服务器,检查您的快速实现,您尝试访问的路由是否在代码中实现