Giter VIP home page Giter VIP logo

base-node-server's Introduction

Nodejs Mongodb Express后台mvc架构基础(入门学习)

前言


本文档针对于像我一样的初学者,自己写了个简单的基础demo学习。原来我是一名本分老实的Android Developer,被朋友带入放荡不羁的Node坑,一入坑再也爬不上来。入门简单强烈推荐会js的朋友入坑(我一个做Android的都入坑,所以,别慌),之前为自己的开源App写的一个小后台(写法极其low,虽然现在也不怎么样),现在稍有改进,以后会继续用简洁优美的写法优化。(以上都是我瞎扯的)

项目介绍

项目使用Express框架和Mongodb数据库

项目使用

1.clone项目

git clone https://github.com/henryzhao/base-node-server

2.依赖模块安装

npm install

3.mongodb数据库安装,启动

安装启动这类教程网上很多,自行查阅

4.启动

npm start

5.自动化文档工具(每次使用需要手动更新接口)

apidoc -i routes/ -o public/apidoc/
(用法参考网址https://www.jianshu.com/p/7e1b057b047c/)

6.测试

http://locahost:3390/apidoc/index.html 就可以成功访问接口了

项目详解

1. 项目入口

app.js

  • express组件初始化
  • 数据库连接
  • 注册路由
  • node服务启动
2. 数据库配置连接

数据库操作(连接、增删改查等)使用mongoose模块,mongoose很好用,我这边只展示最基本的连接操作

const mongoose = require('mongoose');
const dbUrl = 'mongodb://localhost:27017/base-node-server'

exports.connect = () => {
    mongoose.connect(dbUrl, (err) => {
        if(err){
            console.log('数据库连接失败');
        }else{
            console.log('数据库连接成功');
        }
    })
}

mongoose功能远远不止这些, 详细请查看相关api, 中文版貌似机器翻译,不怎么完善,酌情使用,尽量阅读官方api。

3. api主路由

api主路由,所有的api数据功能相关在此汇聚,作为主路由会分发到各个功能模块路由

/**
 * 主路由
 */
const user = require('./user')
const order = require('./order')
const address = require('./address')
const headers = require('./headers')

module.exports = app => {
    //所有的数据请求都需要验证
    app.use(headers.apptype)
    //所有的数据请求都需要验证token
    app.use(headers.token)
    //功能模块子路由
    app.use('/api/user', user)
    app.use('/api/order',order)
    app.use('/api/address',address)
}
4. 子路由

功能子路由,例如:登录、注册操作在此声明汇聚

/**
 * 用户子路由
 */
const express = require('express');
const User = require('../../controller/user/user');
const router = express.Router();

router.post('/register',User.register)
router.post('/login',User.login)

module.exports = router
5. controller逻辑数据处理

逻辑处理、数据库操作都在此处理。 例:用户注册完毕存入数据库,返回前台注册成功等操作

//注册用户
    async register(req, res, next) {
        let username = req.body.username;
        let userpassword = req.body.userpassword;
        try {
            const userinfo = {
                user_name: username,
                user_password: userpassword,
                create_time: dtime().format('YYYY-MM-DD HH:mm')
            }
            await UserModel.create(userinfo)
            res.json({
                code: Constant.RESULT.SUCCESS.code,
                msg: Constant.RESULT.SUCCESS.msg,
                data: '注册成功',
            })
        } catch (err) {
            res.json({
                code: Constant.RESULT.FAILD.code,
                msg: Constant.RESULT.FAILD.msg,
                data: '注册失败',
            })
        }
    }
6. 数据model

这里就是mongoose的强大之处,使用mongoose绑定mondel,然后mondel就可以直接对数据库增删改查,数据库字段、索引等等在此声明。

/**
 * @type 数据库model
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema({
    user_id: Number,
    user_name: String,
    user_password: String,
    user_age: String,
    user_level: String,
    create_time:String
})

//建立索引  提高查询效率
userSchema.index({user_id: 1})

const userinfo = mongoose.model('userinfo', userSchema);

module.exports = userinfo

意见反馈

如果遇到问题或者好的建议,请反馈到:issue 或者[email protected]

如果觉得对你有用的话,赞一下吧!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.