美文网首页
ubuntu搭建express+node+mongodb(mon

ubuntu搭建express+node+mongodb(mon

作者: 笙箫竽笛 | 来源:发表于2019-03-14 10:58 被阅读0次

    Express

    Express 是一个保持最小规模的灵活的 Node.js Web 应用程序开发框架,为 Web 和移动应用程序提供一组强大的功能。

    基于Express的框架有以下

    Feathers: Build prototypes in minutes and production ready real-time apps in days.

    ItemsAPI: Search backend for web and mobile applications built on Express and Elasticsearch.

    KeystoneJS: Website and API Application Framework / CMS with an auto-generated React.js Admin UI.

    Kraken: Secure and scalable layer that extends Express by providing structure and convention.

    LEAN-STACK: The Pure JavaScript Stack.

    LoopBack: Highly-extensible, open-source Node.js framework for quickly creating dynamic end-to-end REST APIs.

    MEAN: Opinionated fullstack JavaScript framework that simplifies and accelerates web application development.

    Sails: MVC framework for Node.js for building practical, production-ready apps.

    Bottr: Framework that simplifies building chatbot applications.

    Hydra-Express: Hydra-Express is a light-weight library which facilitates building Node.js Microservices using ExpressJS.

    Blueprint: a SOLID framework for building APIs and backend services

    Locomotive: Powerful MVC web framework for Node.js from the maker of Passport.js

    graphql-yoga: Fully-featured, yet simple and lightweight GraphQL server

    Express Gateway: Fully-featured and extensible API Gateway using Express as foundation

    Dinoloop: Rest API Application Framework powered by typescript with dependency injection

    Kites: Template-based Web Application Framework

    中文官网:http://www.expressjs.com.cn/


    安装node+express

    1、apt-get预设node repository

    sudo apt-get update
    sudo apt-get install -y python-software-properties software-properties-common
    sudo add-apt-repository ppa:chris-lea/node.js
    sudo apt-get update
    sudo apt-get install nodejs 或者 sudo apt-get install nodejs-legacy

    输入测试 node -v 或者nodejs -v

    2、安装npm

    sudo apt-get install npm

    输入测试npm -v

    3、国内可以安装cnpm

    sudo npm install -g cnpm --registry=https://registry.npm.taobao.org

    4、安装express框架

    sudo npm install express -gd 或者 sudo cnpm install express -gd

    -g代表安装到NODEPATH的lib里面,而-d代表把相依性套件也一起安装。

    如果沒有-g的话会安装目前所在的目录(会建立一个nodemodules的文件夹)

    sudo apt-get install express-generator (sudo apt-get install node-express-generator)

    输入测试express --version

    5、node升级

    sudo npm install -g n #全局安装n模块,用于升级node

    sudo n latest #安装官方最新版本
    sudo n stable #安装官方稳定版本
    sudo n lts #安装官方最新LTS版本

    sudo npm install -g npm #更新npm


    Express项目建立

    1、建立Express项目(example)

    express example

    2、启动项目

    进入example目录,运行服务器:

    npm start


    mongoose安装和使用

    1、安装mongodb

    sudo apt-get install mongodb

    测试:输入mongo

    默认数据存储 /var/lib/mongodb
    默认log文件 /var/log/mongodb/mongodb.log
    默认配置文件 /etc/mongodb.conf

    关闭,开启,重启mongodb服务

    sudo mongodb service stop
    sudo mongodb service start
    sudo mongodb service restart

    配置文件路径:

    sudo vi /etc/mongodb.conf
    dbpath是数据存放的地址修改为你想存放的路径
    logpath是日志存放的地址

    修改配置后需要重启

    sudo mongodb service restart

    2、安装mongoose

    sudo npm install mongoose

    3、例子代码

    建立数据库连接mongo.js文件

    var Mongoose = require("mongoose");    //
    Mongoose.Promise = global.Promise;
    var db Mongoose.createConnection('mongodb://localhost:27017/test');
    
    //链接错误
    db.on('error', function(error) {
       console.log(error);
    });
    
    //链接成功
    db.once('open',function(){
         console.log('Mongoose connection opened');
    });
    
    //链接关闭
    db.on('disconnected', function () { 
       console.log('Mongoose connection disconnected');
    });
      
    module.exports = db;
    

    建立表文件user.js文件

    var Mongoose = require('mongoose');
    var mongo = require('./mongo');
    var ObjectId = Mongoose.Schema.ObjectId;
    
    //=============================================
    //用户记录
    
    var UserSchema = new Mongoose.Schema({
      uid : {type : Number},   //序号
      type : {type : Number, default:0}, 
      name : {type : String},
      birthday   : {type : Date}, //
      status : {type : Number, default:1}
    },{
      versionKey: false, //版本
      timestamps: true  //时间戳createdAt、updatedAt
    });
    
    UserSchema.index({'uid':1}); //建立uid索引,正序
    UserSchema.index({'name':1}); //建立name索引,正序
    var UserModel = mongo.model('users', UserSchema);
    
    module.exports = UserModel;
    

    mongoose API资料:

    https://mongoosejs.com/docs/api.html

    3、mongo升级

    未完待续

    相关文章

      网友评论

          本文标题:ubuntu搭建express+node+mongodb(mon

          本文链接:https://www.haomeiwen.com/subject/jlfqmqtx.html