nodejs+express+mongodb: a simple attempt to write api interface

1: Start mongodb service

My mongoDB installation directory: E:\mongoDB\bin, version: 3.4.9

Open cmd - > e: (enter e disk) - > CD mongoDB / bin (enter bin folder under mongoDB) - > mongod.exe (start mongod first) - > Mongo (then mongo)

 

2: Start the visualization tool of mongoDB, adminMongo

The installation directory of adminMongo is E:\adminMongo, execute the app.js file in the directory, and start the service

 

Directory tree for file:

├── mongodb
│ └── db.js
├── router
│ └── list.js 
├── app.js

db.js

let dbData = {
    selectall: function(name, callback){
        let mongoose = require('mongoose');
        let database_name     = 'mongodb://localhost:27017/abc';
        mongoose.connect(database_name, (err, db) => {
            let collection = db.collection(name);
            collection.find({
                links: {
                    $gt: 10000        //Filter data with a link value greater than 10000
                }
            }).toArray((err, result) => {
                if(err){
                    console.log('error:' + err);
                    return;
                }
                callback(result);
            })
        });
    }
}

module.exports = dbData;

list.js

const express = require('express')
const router = express()
const dbData=require('../mongodb/db.js');

router.post('/', (req, res, next) => {
    dbData.selectall('movie', function(result){
        console.log(result, 111111)
        res.send(result);
    })
})

module.exports = router

app.js

const express = require("express");
const app = express();

// Cross domain setup
app.all("*", function(req, res, next) {
    res.header("Access-Control-Allow-Credentials", true);
    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("Content-Type", "application/json;charset=utf-8");
    next();
});

// Get content
app.use("/list", require("./router/list"));

 app.get('/', (req, res) => {
    res.send('api');
 });

const port = process.env.PORT || 3001;

app.listen(port, () => {
    console.log('Express server listening on port ' + port);
});

module.exports = app;

mongodb's database file structure:

Data in table movie:

Execute node app.js under app.js folder to start node service

 

Data request in project file:

Recently using vue, data request through axios

Request file login.vue

<template>
    <div class="login">
        <header-title text="Sign in" @back="back"></header-title>

        <!--<mt-navbar v-model="selected">
            <mt-tab-item id="1">Sign in</mt-tab-item>
            <mt-tab-item id="2">register</mt-tab-item>
        </mt-navbar>


        <mt-tab-container v-model="selected">

            <mt-tab-container-item id="1" class="login-wrapper">
                <mt-field label="User name" placeholder="enter one user name" v-model="login_username"></mt-field>
                <mt-field label="Password" placeholder="Please input a password" type="password" v-model="login_password"></mt-field>

                <mt-button type="primary" size="large" @click.native="login">Sign in</mt-button>
            </mt-tab-container-item>


            <mt-tab-container-item id="2" class="register-wrapper">
                <mt-field label="User name" placeholder="enter one user name" v-model="register_username"></mt-field>
                <mt-field label="mailbox" placeholder="Please enter email" type="email" v-model="register_email"></mt-field>
                <mt-field label="Password" placeholder="Please input a password" type="password" v-model="register_password1"></mt-field>
                <mt-field label="Confirm password" placeholder="Please confirm the password" type="password" v-model="register_password2"></mt-field>

                <mt-button type="primary" size="large" @click.native="register">register</mt-button>
            </mt-tab-container-item>
        </mt-tab-container>
        -->
        <div class="main">
            <mt-field label="User name" placeholder="Please enter your mobile number" type="tel" v-model="login_username"></mt-field>
            <mt-field label="Password" placeholder="Please input a password" type="password" v-model="login_password"></mt-field>

            <!--<mt-button type="primary" size="large" @click.native="login">Sign in</mt-button>

            <mt-button type="primary" size="large" @click.native="register">register</mt-button>-->

            <div class="login_1 active" @click="login">Sign in</div>

            <div class="register" @click="register">register</div>

            <div class="forgetPassword" @click="forgetPassword">Forget password?</div>
        </div>

        <tab-bar></tab-bar>
    </div>
</template>

<script type="text/ecmascript-6">
    import TabBar            from '@/components/base/tab-bar/tab-bar'
    import HeaderTitle       from '@/components/base/header-bar/header-title'
    import HeaderBar         from '@/components/base/header-bar/header-bar'
    import axios             from 'axios'
    import qs                from 'qs'
    export default {
        components: {
            TabBar,
            HeaderTitle,
            HeaderBar
        },
        data () {
            return {
                selected: '1',
                login_username: '',
                login_password: '',
                register_username: '',
                register_email: '',
                register_password1: '',
                register_password2: '',
            }
        },
        props: {},
        watch: {},
        methods: {
            back () {
                this.$router.push({
                    path: '/mine'
                })
            },
            login () {            // Simulation request here
                axios.post('http://localhost:3001/list', {}, {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
                    .then(function(res){
                        console.log(res)
                    })
                    .catch(function(err){
                        console.log(err)
                    });
             
               
            },
            register () {
                this.$router.push({
                    path: '/register'
                })
            },
            forgetPassword () {
                this.$router.push({
                    path: '/forgetPassword'
                })
            }
        },
        filters: {},
        computed: {},
        created () {

        },
        mounted () {},
        destroyed () {}
    }
</script>

<style lang="less" scoped>
    .login {
        color: #333;
        margin-top: 40px;
        background-color: #fff;
        overflow: visible;
        .main{
            padding: 10px;
            .forget-password{
                background-color: transparent;
                font-size: 12px;
                color: #000;
            }
            .login_1,.register{
                width: 100%;
                height: 40px;
                line-height: 40px;
                text-align: center;
                font-size: 18px;
                color: #3598fe;
                border: 1px solid #3598fe;
                border-radius: 20px;
                margin-top: 20px;
            }
            .active{
                background-color: #3598fe;
                color: #fff;
            }
            .forgetPassword{
                width: 100%;
                height: 30px;
                line-height: 30px;
                text-align: center;
                font-size: 12px;
                margin-top: 10px;
            }
        }
        .login-wrapper {
            margin-top: 20px;
            overflow: hidden;
            .mint-button {
                margin-top: 30px;
            }
        }
        .register-wrapper {
            margin-top: 20px;
            overflow: hidden;
            .mint-button {
                margin-top: 30px;
            }
        }
    }
</style>

Request details:

Tags: Javascript MongoDB axios Mongoose Vue

Posted on Sun, 05 Apr 2020 11:04:22 -0700 by studio805