node服务端
配置转发代理
config/index.js
// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: 'baidu.com',
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8989,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/goods':{
target:'http://localhost:3000'
},
'/goods/*':{
target:'http://localhost:3000'
},
'/users/*':{ // **表示匹配这个路由下面所有的路由接口
target:'http://localhost:3000'
}
},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
server/bin/www
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('server:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
server/models/user.js
var mongoose = require('mongoose')
var userSchema = mongoose.Schema({
"userId":String, // 用户Id
"userName":String, // 用户名
"userPwd":String, // 用户密码
"orderList":Array, // 订单列表
"cartList":[ // 购物车列表
{
"productId": String, // 商品Id
"productName": String, // 商品名称
"salePrice":String, // 商品价格
"productImage":String, // 图片地址
"checked":String, // 是否选中
"productNum":String // 商品数量
}
],
"addressList":Array // 用户地址列表
});
// 通过module.exports进行输出,这样才能加载到 三个参数分别是 模型名,userSchema名,管理数据库集合名
module.exports = mongoose.model("User",userSchema,"users");
server/models/goods.js
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var produtSchema = new Schema({
"productId":String,
"productName":String,
"salePrice":Number,
"checked":String,
"productNum": Number,
"checked":String,
"prodcutImage": String
});
module.exports = mongoose.model('Good',produtSchema);
server/routes/user.js
var express = require('express');
var router = express.Router();
var User = require('./../models/user');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/test', function(req, res, next) {
res.send('test');
});
// 登录
router.post('/login', function(req, res, next) {
// 获取前端传过来的参数 post方式用req.Body形式获取参数
var param = {
userName:req.body.userName,
userPwd:req.body.userPwd
}
User.findOne(param, function(err,doc) {
if(err){
res.json({
status:"1",
msg:err.message
});
}else{
if(doc){
res.cookie("userId",doc.userId,{ // 将用户信息存入cookie
path:'/',
maxAge: 1000*60*60
});
res.cookie("userName",doc.userName, {
path:'/',
maxAge: 1000*60*60
});
// req.session.user = doc; // 将用户信息存入session
res.json({
status:'0',
msg:'',
result:{
userName:doc.userName
}
})
}
}
});
});
// 登出接口
router.post("/logout", function (req,res,next) {
res.cookie("userId", "", { // 登出将userId设置为""
path:"/",
maxAge:-1 // 设置位过期
})
res.json({
status:"0",
msg:'',
result:''
})
})
// 校验用户信息
router.get("/checkLogin", function (req,res,next) {
if(req.cookies.userId){
res.json({
status:'0',
msg:'',
result: req.cookies.userName || '' // 获取cookeie req.cookies.属性
});
}else{ // 取不到就说明当前没有登录
res.json({
status:'1',
msg:'未登录',
result:''
});
}
})
// 查询当前用户的购物车数据
router.get("/cartList", function (req,res,next) {
var userId = req.cookies.userId;
console.log(userId);
User.findOne({userId:userId}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
if(doc){
res.json({
status:'0',
msg:'',
result:doc.cartList
});
}
}
});
});
// 购物车删除
router.post("/cartDel", function (req,res,next) {
// 获取浏览器的cookie 以及用户传递过来的参数 productId
var userId = req.cookies.userId,productId = req.body.productId;
User.update({
userId:userId
},{
$pull:{
'cartList':{
'productId':productId
}
}
}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:'suc'
});
}
});
});
//修改购物车商品数量
router.post("/cartEdit", function (req,res,next) {
var userId = req.cookies.userId, // 获取用户客户端的userId
productId = req.body.productId, // 获取用户传的参数商品id
productNum = req.body.productNum, // 获取用户传的参数商品id
checked = req.body.checked; // 获取用户传的参数是否选中
User.update({
"userId":userId,
"cartList.productId":productId},{
"cartList.$.productNum":productNum,
"cartList.$.checked":checked,
}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:'suc'
});
}
})
});
// 商品全选不选
router.post("/editCheckAll", function (req,res,next) {
var userId = req.cookies.userId,
checkAll = req.body.checkAll?'1':'0';
User.findOne({userId:userId}, function (err,user) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
if(user){
user.cartList.forEach((item)=>{
item.checked = checkAll;
})
user.save(function (err1,doc) {
if(err1){
res.json({
status:'1',
msg:err1,message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:'suc'
});
}
})
}
}
});
});
module.exports = router;
-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》
main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueLazyLoad from 'vue-lazyload' // 引入图片懒加载插件
import infiniteScroll from 'vue-infinite-scroll'
import {currency} from './util/currency' // 引入金额过滤器
Vue.config.productionTip = false
Vue.use(infiniteScroll)
// 使用并配置loading图片
Vue.use(VueLazyLoad,{
loading:"/static/loading-svg/loading-bars.svg"
});
Vue.filter("currency",currency); // 定义全局金额过滤器
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: ' ',
components: { App }
});
金额数据格式化工具
util/currency.js
const digitsRE = /(\d{3})(?=\d)/g
//金额,货币符号,小数点位数
export function currency (value, currency, decimals) {
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : '$'
decimals = decimals != null ? decimals : 2
var stringified = Math.abs(value).toFixed(decimals)
var _int = decimals
? stringified.slice(0, -1 - decimals)
: stringified
var i = _int.length % 3
var head = i > 0
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
: ''
var _float = decimals
? stringified.slice(-1 - decimals)
: ''
var sign = value < 0 ? '-' : ''
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
_float
}
购物车
views/Cart.vue