1 Star 1 Fork 0

凌天akex / NEMT

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

NEMT是什么?

NEMT是基于 NodeJS 的后端架构模板, 是 Node,Express,Mongodb,Template的首字母缩写。NEMT的已经配置常用的功能, 允许你只需要关注于Schema, 当然你也可以追加新功能.

nodejs nodejs nodejs nodejs license release"

使用

  1. git clone https://gitee.com/KYALEX/NEMT.git
  2. cd NEMT && npm i
  3. node app.js

目录设计

  • app.js -------- 入口文件
  • models -------- 数据库模型
  • routes --------- 路由
    • index.js -------- 主路由(自动生成 CURD)
    • login.js -------- 登录
    • upload.js -------- 文件上传
  • utils --------- 工具函数
    • db.js ------ 连接数据库
    • autoCURD.JS ------- 根据模型自动生成 CURD 业务路由
    • encrypt.js ------- 密码加密
    • logger.js ------- 日志
  • configs -------- 配置文件
  • package.json --------- 项目所需依赖
  • logs --------- 日志文件
  • controllers ---------- 业务控制层 数据库 CURD 操作

通用模块

自动路由

反反复复的引入 业务逻辑 挂载到 router 是必须要避免的!!autoCURD.js 封装了通用数据模型的 增删改查操作

const express = require(`express`)
const router = express.Router()
const user = require('../model/User')
const Advertisement = require('../model/Advertisement')
//封装
const ADDROUTER = require('../utils/autoCURD')
//三个参数 挂载路由 , 表名 , 表Model
ADDROUTER(router, 'user', user)
ADDROUTER(router, 'Advertisement', Advertisement)

module.exports = router

图片上传

使用 multer 实现基本的图片上传通用接口, 可拓展文件成上传,如 doc, excel等

const router = require(`express`).Router()
const multer = require('multer')
const adveArtisement = require('../model/Advertisement')
const fs = require('fs')

const storage = multer.diskStorage({
  destination: './public/advertisements',

  filename: function (req, file, cb) {
    console.log(file)
    let extname = file.mimetype.split('/')[1]
    let imgname =
      Date.now() +
      parseInt(Math.random() * 999) +
      parseInt(Math.random() * 2222)
    let keepname = imgname + '广告图片' + '.' + extname
    cb(null, keepname)
  },
})

const upload = multer({
  storage,
})
router.post('/api/v1/uploadimg', upload.single('file'), async (req, res) => {
  console.log(req.body.datas)
  console.log('bodyssssss')
  const datas = JSON.parse(req.body.datas)
  datas.Adimg = req.file.filename
  adveArtisement.create(datas, function (err, doc) {
    console.log('doc', doc)
    if (err) return next(err)
    res.jsonp({
      code: 200,
      data: doc,
    })
  })
})
module.exports = router

注册

  • bcrypt 加密密码,使用 mongdo 提供的 set 模型方法操作
const mongoose = require('mongoose')
const bcypt = require('../utils/encrypt')
const userSchema = new mongoose.Schema({
  user: { type: String, required: true, unique: true },
  pwd: { type: String, required: true, set: value => bcypt(value) }, // 密码加密
  email: String,
  phone: String,
  isaAdmin: { type: Boolean, default: true },
  createtime: { type: Date, default: new Date() }, //创建时间
})

登录


   const re = await User.findOne({ user: user.user })
    if (!re) return res.status(400).send({ code: 400, msg: '用户不存在' })

    // 异步密码比较
    bcrypt.compare(user.pwd, re.pwd).then(valid => {
      if (!valid) return res.status(400).send({ code: 400, msg: '密码错误' })
  • jwt 生成 token响应请求
      const token = jwt.sign({ user }, secret, { expiresIn: '2h' })
      res.json({
        code: 200,
        token,
        msg: '登录成功',
      })
    })
MIT License Copyright (c) 2021 linzhiyu5566 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

express搭建web服务器模板 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/akex9527/NEMT.git
git@gitee.com:akex9527/NEMT.git
akex9527
NEMT
NEMT
master

搜索帮助