安裝nodejs,express,mongodb,mongoose(這裏就很少說了~~)html
express -e project
參數說明:-e:使用的模板類型,project:項目名稱node
cd project
cnpm install
npm run start
app.js 項目的入口文件mongodb
mongo
show dbs
use admin
db
db.createCollection("goods")
show tables
// 引入模塊 var mongoose=require('mongoose'); //引入data.js var seafoodList=require('./data.js').seafoodList // 鏈接數據庫 mongoose.connect('mongodb://localhost:27017/admin') // 獲得數據庫鏈接句柄 var db=mongoose.connection; //經過 數據庫鏈接句柄,監聽mongoose數據庫成功的事件 db.on('open',function(err){ if(err){ console.log('數據庫鏈接失敗'); throw err; } console.log('數據庫鏈接成功') }) //定義表數據結構 var seafoodModel=new mongoose.Schema({ id:Number, name:String, desc:String, discount:Number, price:Number, coin:Number, remain:String, img_sm:String, img_lg:String },{ versionKey:false //去除: - -v }) // 將表的數據結構和表關聯起來 var seafoodModel=mongoose.model("seafoodList",seafoodModel,"goods"); // 添加數據 seafoodModel.insertMany(seafoodList,function(err,result){ if(err){ console.log("數據添加失敗"); throw err; } console.log("數據添加成功:",result); }) //導出數據 module.exports={ seafoodModel:seafoodModel }
//引入express模塊 var express=require("express") //引入數據模塊 var goodsData=require("../model/goods").seafoodModel; //獲取路由對象 var router=express.Router(); //商品管理頁 router.get('/goods',function(req,res){ res.render('index/index') }) //商品信息 router.get('/goodsList',function(req,res){ goodsData.find({},function(err,result){ if(err){ console.log("查詢失敗") throw err } console.log("查詢結果",result) res.render('goods/goodsList',{title:'商品信息',goodsList:result}) }) }) module.exports=router;
<div class="page-content"> <div class="page-header"> <h1> 商品管理 <small> <i class="icon-double-angle-right"></i> <%= title%> </small> </h1> </div><!-- /.page-header --> <!--主要內容--> <div class="row"> <table class="table table-bordered table-hover text-center"> <thead> <tr class="success"> <th class="text-center">id</th> <th class="text-center">name</th> <th class="text-center">desc</th> <th class="text-center">discount</th> <th class="text-center">coin</th> <th class="text-center">remain</th> <th class="text-center">img_sm</th> <th class="text-center">img_lg</th> </tr> </thead> <tbody> <% for(var item in goodsList){%> <tr> <td><%= goodsList[item].id%></td> <td><%= goodsList[item].name%></td> <td><%= goodsList[item].desc%></td> <td><%= goodsList[item].coin%></td> <td><%= goodsList[item].remain%></td> <td><%= goodsList[item].discount%></td> <td class="cut"><%= goodsList[item].img_sm%></td> <td class="cut1"><%= goodsList[item].img_lg%></td> </tr> <% }%> </tbody> </table> </div> </div><!-- /.page-content -->
app.set('views', path.join(__dirname, 'views')); app.engine(".html", ejs.__express); //設置模板引擎類型 app.set('view engine', 'html');