美文网首页
Node+Express+Mongodb+Angular项目搭建

Node+Express+Mongodb+Angular项目搭建

作者: 晨曦Bai | 来源:发表于2020-04-13 16:15 被阅读0次

    https://jingyan.baidu.com/article/0964eca21293a88285f53692.html
    https://www.cnblogs.com/jone-chen/p/6898986.html
    http://www.mamicode.com/info-detail-2612096.html
    express 官网https://www.expressjs.com.cn/en/4x/api.html#req
    mongoose 官方文档
    npm install mongoose
    https://mongoosejs.com/docs/index.html#

    1. 确认已经安装Node.js

    node -v

    2. 创建一个目录,作为当前工作目录

    mkdir meanApp
    cd meanApp

    3. 创建 package.json 文件, 指定入口文件 index.js , 然后一路回车确定

    npm init

    4. 安装 express 并将其保存到依赖列表

    npm install express --save

    5. 创建 MongoDB 数据库

    npm install mongoose --save

    cmd 里使用命令操作数据库

    mongo                                               //   进入数据库
    use express-demo                          // 创建项目数据库
    db.createCollection("users")         //  创建一个集合,即表
    db.users.insert({userid:"admin", password: "123456"})  // 在users 表里添加一条记录
    db.user.find()                                  // 查看users 表
    

    6. 搭建项目原型

    6-1 创建后端express入口文件 app.js , 并复制以下代码
    //  express 导入
    var express = require('express');
    var app = express();
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    // schema,  routes, 
    // mongoose 导入
    var mongoose = require('mongoose')
    
    // 连接数据库mongoose
    mongoose.connect('mongodb://localhost/databasename', { useNewUrlParser: true })
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function() {
    'Connected to database  successfully!'
    });
    
    //  express  使用
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));
    
    // static 文件
    //app.use('/static', express.static(path.join(__dirname, 'public')))
    
    /后台设置跨域访问
    app.all(‘*‘, function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        res.header("X-Powered-By",‘ 3.2.1‘)
        res.header("Content-Type", "application/json;charset=utf-8");
        next();
    });
    
    
    
    
    
    
    module.exports = app;
    
      1. Define schema
        Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
        Each key in our code blogSchema defines a property in our documents which will be cast to its associated SchemaType.
    var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      var blogSchema = new Schema({
        title:  String, // String is shorthand for {type: String}
        author: String,
        body:   String,
        comments: [{ body: String, date: Date }],
        date: { type: Date, default: Date.now },
        hidden: Boolean,
        meta: {
          votes: Number,
          favs:  Number
        }
      });
    

    The permitted SchemaTypes are:

      1. Creating a model

    To use our schema definition, we need to convert our blogSchema into a Model we can work with. To do so, we pass it into mongoose.model(modelName, schema):

    var Blog = mongoose.model('Blog', blogSchema);

    A model is a class with which we construct documents. In this case, each document will be a kitten with properties and behaviors as declared in our schema. Let's create a kitten document representing the little guy we just met on the sidewalk outside:

    var silence = new Kitten({ name: 'Silence' });
    console.log(silence.name); 
    

    相关文章

      网友评论

          本文标题:Node+Express+Mongodb+Angular项目搭建

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